Skip to content

Instantly share code, notes, and snippets.

@thomie
Created June 2, 2011 17:03
Show Gist options
  • Save thomie/1004814 to your computer and use it in GitHub Desktop.
Save thomie/1004814 to your computer and use it in GitHub Desktop.
Self replicating code
x = 'print "x = " + chr(39) + x + chr(39) + "; exec(x)"'; exec(x)
#include <stdio.h>
#define CODE "#include <stdio.h>%c#define CODE %c%s%c%cint main(void){ return printf(CODE, 10, 34, CODE, 34, 10, 10); }%c"
int main(void){ return printf(CODE, 10, 34, CODE, 34, 10, 10); }
#include <stdio.h>
#define CODE int main(void){ return printf("#include <stdio.h>\n\n#define CODE %s\n\n/* http://gcc.gnu.org/onlinedocs/cpp/Stringification.html */\n#define XQUOTE(x) QUOTE(x)\n#define QUOTE(x) #x\n\nconst char* SOURCE = XQUOTE(CODE);\nCODE\n", SOURCE); }
/* http://gcc.gnu.org/onlinedocs/cpp/Stringification.html */
#define XQUOTE(x) QUOTE(x)
#define QUOTE(x) #x
const char* SOURCE = XQUOTE(CODE);
CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment