Created
August 15, 2012 13:06
-
-
Save ibaned/3359954 to your computer and use it in GitHub Desktop.
My solution to the quine problem: a program that prints itself
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
void p(const char*c,int d) | |
{ | |
for(;d;++c,--d) | |
putchar(*c); | |
} | |
void q(const char*c,int d) | |
{ | |
for(;d;++c,--d) | |
if(*c=='\n') | |
printf("\\n"); | |
else if(*c=='\"') | |
printf("\\\""); | |
else if(*c=='\\') | |
printf("\\\\"); | |
else | |
putchar(*c); | |
} | |
int main() | |
{ | |
const char*a="#include<stdio.h>\n#include<string.h>\nvoid p(const char*c,int d)\n{\nfor(;d;++c,--d)\nputchar(*c);\n}\nvoid q(const char*c,int d)\n{\nfor(;d;++c,--d)\nif(*c=='\\n')\nprintf(\"\\\\n\");\nelse if(*c=='\\\"')\nprintf(\"\\\\\\\"\");\nelse if(*c=='\\\\')\nprintf(\"\\\\\\\\\");\nelse\nputchar(*c);\n}\nint main()\n{\nconst char*a=\"\";\np(a,285);\nq(a,333);\np(a+285,48);\nreturn 0;\n}\n"; | |
p(a,285); | |
q(a,333); | |
p(a+285,48); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is probably not a nice or short solution, almost all the code deals with C escape sequences. The string.h may not even be needed...