newexpert
asked on
Just for fun III - Prints self out ...
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 ...
Objective: a suitable add-on for copyright software? (just kidding)
There is a solution ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);}
char*s="char*s=%c%s%c;main
that is a bit shorter :)
ASKER
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
#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
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");}'
#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");}'
ASKER
Good point. Wonder how hard would it be to create a .h which source dump any .c file that includes it?
ASKER
Where did the last word "it" go?
binary???