Have you ever tried writing a C code that prints the source itself. Its a little bit tricky anyway. But the solution is here
#include
char *program = "#include %c char *program = %c%s%c;%c int main()%c{%cprintf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c return 0;%c}%c";
int main()
{
printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);
return 0;
}
Another program can be written in a single line
main(a){a="main(a){a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}
Assumptions:
- The program is run on an ASCII machine.
- No newline is needed at the end of the source.
- char * does not lose any information when converted to int.
- int has the same bit representation, size, and argument passing convention as char *.
- A random return value from main does not cause problems.
- printf works without prototype.