Skip to content

Instantly share code, notes, and snippets.

@archagon
Created June 14, 2012 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archagon/2932507 to your computer and use it in GitHub Desktop.
Save archagon/2932507 to your computer and use it in GitHub Desktop.
My first quine.
#include <stdio.h>
#include <string.h>
int main(void)
{
char* code = "#include <stdio.h>\n#include <string.h>\n\nint main(void)\n{\n char* code = \"%s\";\n\n int len = strlen(code);\n int tokenStarted = 0;\n for (int i = 0; i < len; i++)\n {\n char c = code[i];\n\n if (tokenStarted == 1)\n {\n if (c == 's')\n {\n for (int j = 0; j < len; j++)\n {\n char d = code[j];\n\n if (d == '\\n')\n {\n putchar('\\\\');\n putchar('n');\n }\n else if (d == '\\\"')\n {\n putchar('\\\\');\n putchar('\\\"');\n }\n else if (d == '\\\\')\n {\n putchar('\\\\');\n putchar('\\\\');\n }\n else\n {\n putchar(d);\n }\n }\n }\n else\n {\n putchar('%');\n putchar(c);\n }\n tokenStarted = 0;\n }\n else if (c == '%')\n {\n tokenStarted = 1;\n }\n else\n {\n putchar(c);\n }\n }\n}\n";
int len = strlen(code);
int tokenStarted = 0;
for (int i = 0; i < len; i++)
{
char c = code[i];
if (tokenStarted == 1)
{
if (c == 's')
{
for (int j = 0; j < len; j++)
{
char d = code[j];
if (d == '\n')
{
putchar('\\');
putchar('n');
}
else if (d == '\"')
{
putchar('\\');
putchar('\"');
}
else if (d == '\\')
{
putchar('\\');
putchar('\\');
}
else
{
putchar(d);
}
}
}
else
{
putchar('%');
putchar(c);
}
tokenStarted = 0;
}
else if (c == '%')
{
tokenStarted = 1;
}
else
{
putchar(c);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment