Skip to content

Instantly share code, notes, and snippets.

@ibaned
Created August 15, 2012 13:06
Show Gist options
  • Save ibaned/3359954 to your computer and use it in GitHub Desktop.
Save ibaned/3359954 to your computer and use it in GitHub Desktop.
My solution to the quine problem: a program that prints itself
#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;
}
@ibaned
Copy link
Author

ibaned commented Aug 15, 2012

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment