C preprocessor power
Filed Under Software, Mind benders
I was discussing with a friend the popular chalange to write a program that prints its own source. In C of course. The shortest one we found was:
main(){char*s=”main(){char*s=%c%s%c;printf(s,34,s,34);}”;printf(s,34,s,34);}
What I did not like about this one is that it relied on the knowledge that the ASCII code of the quote is 34. I wanted to find a solution that is still short, but does not have that. Preprocessor to the rescue:
#define _(__) char*a=#__;__
_(main() {printf(”#define _(__) char*a=#__;__\n_(%s)”,a);})
Post Linx
Permalink | Trackback |
|
Print This Page | Leave a Comment

























