My attempt at a
quine* in C, created while waiting for a delayed plane on KRK airport. It turned out to be easier than I expected.
* A program that writes out its own source code. It's said to be one of the classic exercises for programmers.
The source itself:
#include <stdio.h>
const char *f(const char *t) {
static char buf[1024 * 1024];
char *p = buf;
while(*t) {
switch(*t) {
case 0x0a: *(p++) = 0x5c; *(p++) = 'n'; break;
case 0x22: *(p++) = 0x5c; *(p++) = 0x22; break;
default: *(p++) = *t;
}
t++;
}
return buf;
}
int main(void) {
const char *s = "#include <stdio.h>\nconst char *f(const char *t) {\n static char buf[1024 * 1024];\n char *p = buf;\n while(*t) {\n switch(*t) {\n case 0x0a: *(p++) = 0x5c; *(p++) = 'n'; break;\n case 0x22: *(p++) = 0x5c; *(p++) = 0x22; break;\n default: *(p++) = *t;\n }\n t++;\n }\n return buf;\n}\nint main(void) {\n const char *s = \"%s\";\n printf(s, f(s));\n return 0;\n}\n";
printf(s, f(s));
return 0;
}