The term “Quine” refers to a special kind of Program (or shall I call Meta program?) that outputs its own complete source code as its only output. It is termed after the name of a philosopher Willard Van Orman Quine who did great study of indirect self references. There are certain distinct characteristics of what can be considered as Quine and what not.
A Quine can typically be written in any programming language that can compute and print a string – that means any programming language. Although in few it will be more convenient than others.
As per the definition the sole output of a Quine is its own source code there are few restrictions in defining the program as Quine.
- You are not supposed to perform a general File I/O to read and dump the source code. It can be safely assumed that the source code file is not available.
- Quine is not supposed to take any input. That way anybody can re-type the entire source code.
- There should be some code. Once a programmer played around the above to rules and wrote the shortest Quine – An empty program having nothing and giving the same output that is nothing. Clever Trick. That brought this third rule in place.
So how exactly do we define a Quine? A Quine will generally have two parts –
- Code to print the output.
- Data part that represents the program code as data.
A simple Quine in C language will be something like the code below:
main() { char *s="main() { char *s=%c%s%c; printf s,34,s,34); }"; printf(s,34,s,34); }
A Quine seem to serve no real purpose than to serve as a good programming puzzle to try out. Languages supporting proper reverse engineering and reflection certainly will provide a better way to write Quine.
I will not dive into historical evolution of Quine; but Wikipedia and Google will give a lot more material to those interested.