C preprocessor power

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);})

Share

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>