A program that generates its own source code as its output. The mechanism is the same as used in the English sentence:

"preceded by itself in quotes." preceded by itself in quotes.

Named after Willard van Orman Quine, by Douglas Hofstadter.

"'Is a quine' is a quine" is a quine, albeit untruthful. "'Is not a quine' is not a quine" is a quine, and is also true.

"Is a statement without a subject" is a statement without a subject.

"Would be a silly name to give a child" would be a silly name to give a child.

And furthermore.

"gives resc food for thought" gives resc food for thought

To manufacture a computer quine, a program which prints its own source code, you might do this:

  1. Write a function or subroutine or whatever which, given input, prints it twice: once in the syntax of a data structure which contains the input, and once as just the input.
  2. Feed the function to itself.
Doric word meaning "girl" or "woman". See also lass.

See the Everything Guide to Doric Words and Phrases.
If you take quines away from computing for a second they become even more interesting. Good ol'Dougie Hofs gives a few examples in GEB, including:

"is made up of six words" is made up of six words.

Which is much more profound than it seems at first sight, and is a useful first step towards looking at self referential stuff like "this statement cannot be proven". It shows what the "this" is really doing, ie its inserting the reference to the rest of the sentence.

quine, v. To deny resolutely the existence or importance of something significant. - The Philosophical Lexicon (8th edition, 1987). Yes, 'quine' is a verb as well! This eccentric and satirical usage comes from W. V. O. Quine's tendency to deny the existence of things in this way (for example, it has been claimed of his philosophy that it denies the existence of beliefs).

The classic use of this term may be in Daniel Dennett's 'Quining Qualia' (in Consciousness in Contemporary Science, eds. Marcel and Biasch, OUP); a paper where Dennett argues that subjective experience is not 'special' in the ways that other thinkers have presumed (i.e., the ways in which qualia are special).

The word "quine" is now used to refer to two separate concepts - a phrase that quotes itself, and a phrase that reproduces himself. I believe Douglas Hofstadter distinguished these as "self-ref" and "self-rep".

Most self-reps are self-refs. Most self-refs are not self-reps. Additionally, in computer languages, the word "quine" is almost exclusively used to refer to self-reps, even the ones that work by some mechanism other than quining (quoting themselves).

Most of the English sentences above are self-refs, and the computer examples are self-reps. '"preceded by itself in quotes." preceded by itself in quotes.' is both an English quine and a self-rep, because it actually does thoroughly describe itself.

For the sake of comparing what is and is not a self-rep:

  • The Befunge code (this language was selected because it has really short quines) <@,g09,k9"
    This program describes (in fact, it outputs) a certain line of text, which happens to be <@,g09,k9" - which is the same as the code. This is a self-rep. Because it works by making two passes over itself, one of which is in 'quotes', it could be considered a self-ref as well.
  • "preceded by itself in quotes." preceded by itself in quotes.
    This phrase describes a certain phrase, which happens to be "preceded by itself in quotes." preceded by itself in quotes. - which is the same as the phrase. This is a self-ref and a self-rep.
  • 'is not a quine' is not a quine
    This sentence describes a feature of the phrase 'is not a quine'. A sentence is not the same as a feature of part of that sentence. Sure, you could create a sentence which describes that feature. Then, by definition, you get the original sentence or something very similar to it back. "My name is Bob" describes a feature of my name, which is that it is Bob, but in doing so it does not describe the sentence "My name is Bob". So neither does this, in any way, describe the sentence "'is not a quine' is not a quine". So this sentence is a quine, but only by Hofstadter's definition, not by the same definition used for computer programs.
    In fact, I think it would be rather difficult for a complete sentence to be a self-rep. Sentences describe concepts, but sentences are objects. On the other hand, noun phrases describe objects, and noun phrases are objects - noun phrases have a shot at being a self-rep. This is why command-line computer programs can be quines, as well - a command-line computer program's source code describes its output, which is made of lines of text, and a command-line computer program's source code itself is made of lines of text.

I've revised this node after discovering that 'quine' was originally used to mean self-ref and not self-rep. After changing some words, the points before still stand, but the closing statement that I rather enjoyed writing no longer holds:

In summary: "'is not a quine' is not a quine" is not a quine. And neither is the previous sentence.

This is not exactly a quine, but is a neat quine-related curiosity:
int n=103;char*f="int n=%d;char*%c=%c%s%c;
main(){printf(%c,205-n,n,34,%c,34,n,n,10);}%c";
main(){printf(f,205-n,n,34,f,34,n,n,10);}
This code (newlines removed) prints out an other C program, which in turn prints out the original code (I could've equally supplied this code's output).

This is not a bi-quine nor a multi-quine, I'm not sure what to call it.

quick-and-dirty = Q = quote chapter and verse

quine /kwi:n/ n.

[from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. (We ignore some variants of BASIC in which a program consisting of a single empty string literal reproduces itself trivially.) Here is one classic quine:

((lambda (x)
  (list x (list (quote quote) x)))
 (quote
    (lambda (x)
      (list x (list (quote quote) x)))))

This one works in LISP or Scheme. It's relatively easy to write quines in other languages such as Postscript which readily handle programs as data; much harder (and thus more challenging!) in languages like C which do not. Here is a classic C quine for ASCII machines:

char*f="char*f=%c%s%c;main()
{printf(f,34,f,34,10);}%c";
main(){printf(f,34,f,34,10);}

For excruciatingly exact quinishness, remove the interior line breaks. Here is another elegant quine in ANSI C:

#define q(k)main(){return!puts(#k"\nq("#k")");}
q(#define q(k)main(){return!puts(#k"\nq("#k")");})

Some infamous Obfuscated C Contest entries have been quines that reproduced in exotic ways. There is an amusing Quine Home Page.

--The Jargon File version 4.3.1, ed. ESR, autonoded by rescdsk.

Examples of quines in Perl 5

This first example, while a valid quine, is considered "cheating" by those in the know , since it opens itself and simply scans its own source code:

#! /usr/bin/perl -w

# This is not really a quine:

open (Q, $0);
while (<Q>) {
    print;
}

This next example is considered a quine amongst the Perl community, but it still seems like cheating to me, using a syntactic trick of Perl to do it's job. It takes advantage of the fact that Perl will scan the __DATA__ structure before it is even declared:

#! /usr/bin/perl

undef $/;
$_ = <DATA>;
print;
print;

__DATA__
#! /usr/bin/perl

undef $/;
$_ = <DATA>;
print;
print;
__DATA__

Here is another example that is a little more fun, courtesy of Blake Mills (Blakem on perlmonks):

#!/usr/bin/perl

printme(q{
sub printme {
  print "#!/usr/bin/perl\n\n";
  print "printme(q{$_[0]});\n";
  print "$_[0]";
}
});

sub printme {
  print "#!/usr/bin/perl\n\n";
  print "printme(q{$_[0]});\n";
  print "$_[0]";
}

Now is where it starts getting really interesting. Try and GOLF your own in any language!

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.

Log in or registerto write something here or to contact authors.