Skip to content

Instantly share code, notes, and snippets.

@tobbez
Created March 19, 2010 01:04
Show Gist options
  • Save tobbez/337113 to your computer and use it in GitHub Desktop.
Save tobbez/337113 to your computer and use it in GitHub Desktop.
C program that writes out its own source
int X = 184;void w(char *s){int i=0;for(i=0;i<strlen(s);++i) if (s[i] == '"' || s[i] == '\\') { write(1,"\\",1); write(1,s+i,1); } else { write(1,s+i,1); }} int main(void){char s[] = "int X = 184;void w(char *s){int i=0;for(i=0;i<strlen(s);++i) if (s[i] == '\"' || s[i] == '\\\\') { write(1,\"\\\\\",1); write(1,s+i,1); } else { write(1,s+i,1); }} int main(void){char s[] = \"\"; write(1, s, X); w(s); puts(s+X); return 0; }"; write(1, s, X); w(s); puts(s+X); return 0; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment