Skip to content

Instantly share code, notes, and snippets.

@sortie
Created September 5, 2016 20:01
Show Gist options
  • Select an option

  • Save sortie/f78f43c7197d84ee90e346bae4d8510c to your computer and use it in GitHub Desktop.

Select an option

Save sortie/f78f43c7197d84ee90e346bae4d8510c to your computer and use it in GitHub Desktop.
quine
#include <stdio.h>
static const char code[] =
"#include <stdio.h>\n"
"\n"
"static const char code[] =\n"
" \"\0\";\n"
"\n"
"int main(void) {\n"
" for (size_t i = 0; i < sizeof(code) - 1; i++) {\n"
" if (code[i] == '\\0') {\n"
" for (size_t j = 0; j < sizeof(code) - 1; j++) {\n"
" if (code[j] == '\\0')\n"
" printf(\"\\\\0\");\n"
" else if (code[j] == '\\\\')\n"
" printf(\"\\\\\\\\\");\n"
" else if (code[j] == '\"')\n"
" printf(\"\\\\\\\"\");\n"
" else if (code[j] == '\\n')\n"
" printf(\"\\\\n\\\"\\n \\\"\");\n"
" else\n"
" putchar((unsigned char) code[j]);\n"
" }\n"
" } else {\n"
" putchar((unsigned char) code[i]);\n"
" }\n"
" }\n"
" return 0;\n"
"}\n"
"";
int main(void) {
for (size_t i = 0; i < sizeof(code) - 1; i++) {
if (code[i] == '\0') {
for (size_t j = 0; j < sizeof(code) - 1; j++) {
if (code[j] == '\0')
printf("\\0");
else if (code[j] == '\\')
printf("\\\\");
else if (code[j] == '"')
printf("\\\"");
else if (code[j] == '\n')
printf("\\n\"\n \"");
else
putchar((unsigned char) code[j]);
}
} else {
putchar((unsigned char) code[i]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment