A quine, as every good programmer should know, is a program (or sentence) that produces itself when run, without using its own source. They are named after Willard Van Orman Quine, a philosopher who studied self-reference. From HQ9+'s
Q
command, to the everlasting
print "print "print "print "print...
there are a lot of them, and sadly, most of them are in the form of
meaningless blocks of code, because
writing clever bits of code makes you look cool.
However, you should know:
So let us make a quine, to see what makes it tick.
Note that we are going for understanding, rather than conciseness, so if you want large globs of unintelligible code to run there are plenty out there (see the link at the bottom).
The easiest type of quine to do is one that simply loads its own source, and prints that:
int main() {
FILE *f = fopen("/path", "r");
char c;
while (c = (getc(f))) putchar(c);
fclose(f);
}
But this is considered cheating by many as it goes against the rule of not using its source. Besides, this does not work on compiled programs.
All proper quines must contain the same code twice: one to execute, and another in a string to print out. This program can print out part of it:
int main() {
int a = 0;
printf("int a = 0;\n");
}
This looks simple enough, so far. The most difficult task, though, is printing the command that prints - as you can imagine, you would have to print the command that prints the command that prints the command, and so on ad infinitum.
int main() {
char *s = "int main(){ char *s = \"int main(){ char *s = \\\"int main(){ char *s.......";
printf(s);
}
The string s is going to be pretty big, if we keep on going like that way. And even if you could have it infinitely long, it would take ages to download from the Internet.
The thing to do here is put the string in itself. By using %s in a string, with the string as its argument, we can encode the string inside itself, giving us a string containing the entire program's code, which we can then merrily printf out.
int main() {
char *s = "int main(){char*s=\"%s\"; printf(\"s,s\");}";
printf(s, s);
}
If you cannot tell what is going on here, think of the printf statement as:
printf("int main(){ char*s=\"%s\"; printf(\"s, s\"); }", s);
It prints the program, including the command that sets s to %s, which in this case is itself.
So are we done? Not quite. If you try running the code, it prints the string s fine. However, if you try running the code that that produces, it doesn't quite compile. Oh dear.
What's wrong? The generated program, although it looks fine, does not escape the quotes in the string - it uses " instead of \", and the C compiler chokes on it. We cannot escape them, as then the original program would not be correct.
Instead, we must print the character (ASCII code 34) with a printf %c, instead of actually quoting it in the string, as well as adding it to the printf statement. This way, the quotes are carried through into the source that the program generates.
int main() {
char *s = "int main() { char *s = %c%s%c; printf(s, 34, s, 34); }";
printf(s, 34, s, 34);
}
Echo it, compile it, and run it. Then run that, and, if you want, run what that produces as well. Run it as many times as you like, you'll get the same thing. Which means we finally have our quine, yay!
Variants on this method include printing a series of character codes instead of a string, or running exec() on a string to print it out. Some programs manage to produce a quine slightly different from the original. For examples, as well as other eschewed code, see the Obfuscated C Contest.
Now that they look a lot less like blocks of meaningless code now, why don't you try a few others? http://www.nyx.net/~gthompso/quine.htm contains the list.