In my course slides, I have this example but without much explanation:
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
I understand what quine programs in general mean but I do not quite understand what's happening in the code above. This is the output I get if I run it:
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
But how is it reproducing its own code? I don't really understand how the output is produced.
Start by writing it out in a way that'll be clearer (not changing anything but the layout):
So we see a
mainfunction as we'd expect (it should return anintbut you're allowed to get away with not in C; likewise for no function arguments). And before that, a regular string. It's a funny-looking string, but it is not really that different tochar*f="fish";.Okay, so what if we expand the
printfby putting the string in there by hand?We can see that it's going to print out some guff, and substitute in some values along the way. They are:
Let's substitute those all in then too (though I've replaced the contents of the string with
<the string>, and"'s with\"'s to make it actually work as a standalone statement):Well look at that!
mainsimply prints out the line we first started with. Hurrah!Edited to add:
Although I've basically spelled out the answer for you, there is still a puzzle remaining. Consider for yourself why we bother substituting in the
34, f, 34, 10, rather than just putting them directly into the string like I did in my final code.