Quine Article Index for
Quine
Articles about
Quine
Website Links For
Quine
 

Information About

Quine




In Computing , a quine is a Program (a form of Metaprogram ) that produces its complete Source Code as its only output. For amusement, Hackers sometimes attempt to develop the shortest possible quine in any given Programming Language .

Note that programs which take input are not considered quines. This would allow the source code to be fed to the program via keyboard input, opening the source file of the program, and similar mechanisms. Also, a quine which contains no code is ruled out as trivial; in many programming languages executing such a program will output the code (i.e. nothing). Such an empty program once won the "worst abuse of the rules" prize in the Obfuscated C contest (indeed, an empty program is not legal ISO C since it has no main() function, so many compilers will reject it).

Quines are named after philosopher Willard Van Orman Quine (1908–2000), who made an extensive study of Indirect Self-reference : he coined, among others, the paradox-producing expression, "yields falsehood when appended to its own quotation."


HISTORY

A quine exists in any programming language with the ability to output any computable string, as a direct consequence of Kleene's Recursion Theorem . The specific idea of quines first appeared in ''Bratley, Paul and Jean Millo. "Computer Recreations; Self-Reproducing Automata", Software -- Practice & Experience, Vol. 2 (1972). pp. 397-400.'' Bratley first became interested in self-reproducing programs after seeing the first known such program written in Atlas Autocode at Edinburgh in the 1960s by the University Of Edinburgh lecturer and researcher Hamish Dewar. This program appears below:

%BEGIN
!THIS IS A SELF-REPRODUCING PROGRAM
%ROUTINESPEC R
R
PRINT SYMBOL(39)
R
PRINT SYMBOL(39)
NEWLINE
%CAPTION %END~
%CAPTION %ENDOFPROGRAM~
%ROUTINE R
%PRINTTEXT '
%BEGIN
!THIS IS A SELF-REPRODUCING PROGRAM
%ROUTINESPEC R
R
PRINT SYMBOL(39)
R
PRINT SYMBOL(39)
NEWLINE
%CAPTION %END~
%CAPTION %ENDOFPROGRAM~
%ROUTINE R
%PRINTTEXT '
%END
%ENDOFPROGRAM


EXAMPLES

''For more examples, see ''

; C
  • A simple quine (self-printing program), in standard C. ---/


  • Note: in designing this quine, we have tried to make the code clear

  • and readable, not concise and obscure as many quines are, so that

  • the general principle can be made clear at the expense of length.

  • In a nutshell: use the same data structure (called "progdata"

  • below) to output the program code (which it represents) and its own

  • textual representation. ---/


#include

  • s)

  • This function takes a character string s and prints the

  • textual representation of s as it might appear formatted in C

  • code. ---/

  • {

int i;

printf(" \"");
for (i = 0; s {Link without Title} ; i++) {

printf("\\");
else if (s {Link without Title} == '"')
printf("\\"");
else if (s {Link without Title} == '
')
printf("\n");
  • Others are just printed as such. ---/

  • else

printf("%c", s {Link without Title} );
  • Also insert occasional line breaks. ---/

  • if (i % 48 == 47)

printf("\"
\"");
}
printf("\"");
}

  • What follows is a string representation of the program code, from

  • beginning to end (formated as per quote() function, above), except

  • that the string _itself_ is coded as two consecutive '@'

  • characters. ---/

  • const char progdata {Link without Title} =

  • A simple quine (self-printing program), in st"

  • /


  • Note: in designing this quine, "

  • "we have tried to make the code clear

  • and read"

  • "able, not concise and obscure as many quines are"

", so that
  • the general principle can be made c"

  • "lear at the expense of length.

  • In a nutshell:"

  • " use the same data structure (called \"progdata\"

"
  • below) to output the program code (which it r"

  • "epresents) and its own

  • textual representation"

  • /


#include

void quote(const char "
  • s)

  • This function takes a character stri"

  • "ng s and prints the

  • textual representati"

  • "on of s as it might appear formatted in C

"
  • code. ---/

  • {

int i;

printf(\" \\"\");
"
" for (i = 0; s {Link without Title} ; i++) {

"')
printf(\"\\\\\");
else if ("
"s {Link without Title} == '\"')
printf(\"\\\\"\");
"
" else if (s {Link without Title} == '\n')
printf(\"\\n\""
");
  • Others are just printed as such. ---"

  • "/

else
printf(\"%c\", s {Link without Title} );
"
  • Also insert occasional line breaks. ---/"

  • "

if (i % 48 == 47)
printf(\"\"
"\"\n \\"\");
}
printf(\"\\"\");
}

  • What "

  • "follows is a string representation of the progra"

"m code, from
  • beginning to end (formated as pe"

  • "r quote() function, above), except

  • that the s"

  • "tring _itself_ is coded as two consecutive '@'

"

@@;

in"
"t main(void)
  • The program itself... ---/

  • {

"
" int i;

  • Print the program code, chara"

  • /

  • for (i = 0; progdata[i"

"]; i++) {
if (progdata {Link without Title} == '@' && prog"
"data + 1 == '@')
  • We encounter "

  • "two '@' signs, so we must print the quoted

"
  • form of the program code. ---/

  • {"

"
  • Quote all. ---"

  • "/

  • Skip second"

  • /

  • } else

printf(\"%c\","
  • Print character. ---/

  • }

"
" return 0;
}
";

int main(void)
  • The program itself... ---/

  • {

int i;

  • Print the program code, character by character. ---/

  • for (i = 0; progdata {Link without Title} ; i++) {


'@' && PROGDATA[I + 1

  • We encounter two '@' signs, so we must print the quoted

  • form of the program code. ---/

  • {

  • Quote all. ---/

  • Skip second '@'. ---/

  • } else

  • Print character. ---/

  • }

return 0;
}

; Scheme
((lambda (x)
(list x (list (quote quote) x)))
(quote
(lambda (x)
(list x (list (quote quote) x)))))


SEE ALSO



FURTHER READING

  • ''



EXTERNAL LINKS