Solved

Just for fun III - Prints self out ...

Posted on 1998-08-19
8
180 Views
Last Modified: 2010-04-15
Simple exercise: Program that prints itself out (Only executable version is available at runtime!!!)

Objective: a suitable add-on for copyright software? (just kidding)

There is a solution ...
0
Comment
Question by:newexpert
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1252176
Are you talking about printing source code? or the
binary???  
0
 
LVL 6

Accepted Solution

by:
snoegler earned 0 total points
ID: 1252177
#include <stdio.h>
char *text[]={"char *text[]={","%s%c%s%c,%c%s%c,%c%s%c,%c%s%c,%c%s%c,%c%s%c};%c%c","void main(){","puts(text[5]);printf(text[1],text[0],34,text[0],34,34,text[1],34,34,text[2],34,34,text[3],34,34,text[4],34,34,text[5],34,13,10);","puts(text[2]);puts(text[3]);printf(text[4]);}","#include <stdio.h>"};
void main(){
puts(text[5]);printf(text[1],text[0],34,text[0],34,34,text[1],34,34,text[2],34,34,text[3],34,34,text[4],34,34,text[5],34,13,10);
puts(text[2]);puts(text[3]);printf(text[4]);}

I've tested it, it works - but it's pretty ugly :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252178
The standard example, fron the comp.lang.c FAQ, is:

char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 6

Expert Comment

by:snoegler
ID: 1252179
that is a bit shorter :)
0
 
LVL 1

Author Comment

by:newexpert
ID: 1252180
How about the one I had in mind (it even does some extra things):
#include "pself.h"
PH(int main(void) {)
PA(int i; int sum = 0; for (i = 1; i <= 100; i++) sum += i;)
PB(return sum; })

The actual code that does the hard work is conveniently hidden in pself.h
#define PH(x) x puts("#include \"pself.h\"\nPH("#x")");
#define PA(x) x; puts("PA("#x")");
#define PB(x) puts("PB("#x")"); x

0
 
LVL 84

Expert Comment

by:ozo
ID: 1252181
Hmm, if you allow that, then how about just this:

#include "pself.h"

with pself.h containg:
main(){
  int i; int sum = 0;
  for (i = 1; i <= 100; i++) sum += i;
  puts("#include \"pself.h\"\n");
  return sum;
}

Or this:

X

compiled with
cc -DX='main(){puts("X");}'


0
 
LVL 1

Author Comment

by:newexpert
ID: 1252182
Good point.  Wonder how hard would it be to create a .h which source dump any .c file that includes it?
0
 
LVL 1

Author Comment

by:newexpert
ID: 1252183
Where did the last word "it" go?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question