This is an article about a kind of computer program. You may be looking for information on the philosopher W. V. 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 simply opening the source file of the program and printing the contents is considered cheating.
Quines are named after W. V. Quine, 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."
1 Sample quine in C
#include
char*i="\\#include",n='\n',q='"',*p=
"%s%cchar*i=%c%c%s%c,n='%cn',q='%c',*p=%c%c%s%c,*m=%c%c%s%c%c;%s%c",*m=
"int main(){return!printf(p,i+1,n,q,*i,i,q,*i,q,n,q,p,q,n,q,m,q,n,m,n);}"
;int main(){return!printf(p,i+1,n,q,*i,i,q,*i,q,n,q,p,q,n,q,m,q,n,m,n);}
2 Sample quine in C#
(Note: linebreaks added for ease of reading)
using System;
namespace quine
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string s = "using System;{0}namespace quine{0}{2}{0}{1}class Program{0}
{1}{2}{0}{1}{1}[STAThread]{0}{1}{1}static void Main(string[] args){0}{1}{1}{2}{0}{1}{1}{1}
string s = {4}{6}{4};{0}{1}{1}{1}Console.Write(s, Environment.NewLine, {4}{5}t{4}, {4}{2}
{4}, {4}{3}{4}, {4}{5}{4}{4}, {4}{5}{5}{4}, s);{0}{1}{1}{3}{0}{1}{3}{0}{3}";
Console.Write(s, Environment.NewLine, "\t", "{", "}", "\"", "\\", s);
}
}
}
3 Sample quine in Scheme
((lambda (x)
(list x (list (quote quote) x)))
(quote
(lambda (x)
(list x (list (quote quote) x)))))
(funcall (lambda (x)
(append x (list (list 'quote x))))
'(funcall (lambda (x)
(append x (list (list 'quote x))))))
5 Sample quine in OCaml
(fun s -> Printf.printf "%s %S" s s) "(fun s -> Printf.printf \"%s %S\" s s)"
6 Sample quine in Python
a='a=%s;print a%%`a`';print a%`a`
Another:
b='\\';g='"';p='%';s="b='%s%s';g='%s';p='%s';s=%s%s%s;print s%s(b,b,g,p,g,s,g,p)";print s%(b,b,g,p,g,s,g,p)
And another one that shares the last 61 characters with the previous one (just to show that multiple assignments don't save typing :)):
b,g,p,s='\\','"','%',"b,g,p,s='%s%s','%s','%s',%s%s%s;print s%s(b,b,g,p,g,s,g,p)";print s%(b,b,g,p,g,s,g,p)
unescape(q="unescape(q=%22*%22).replace('*',q)").replace('*',q)
8 Sample quine in Perl
$_=q{$_=q{Q};s/Q/$_/;print};s/Q/$_/;print
And a shell/Perl combo:
perl -le '$n=q{perl -le a$n=q{$x};($_=$n)=~s/\141/\47/g;s/\$x/$n/;printa};($_=$n)=~s/\141/\47/g;s/\$x/$n/;print'
9 Sample quine in BASICBASIC is a family of high-level programming languages. Originally devised as an easy-to-use tool, it became widespread on home microcomputers in the 1980s, and remains popular to this day in a handful of heavily evolved dialects. BASIC's name, coined in c
10 C=": PRINT CHR(49)+CHR(48)+CHR(32)+CHR(67)+CHR(61)+CHR(34)+C+CHR(34)+C":
PRINT CHR(49)+CHR(48)+CHR(32)+CHR(67)+CHR(61)+CHR(34)+C+CHR(34)+C
10 Sample quine in PascalPascal is one of the landmark computer programming languages on which generations of students cut their teeth and variants of which are still widely used today. TeX and much of the original Macintosh operating system were written in Pascal. The Swiss comp
const a='const a=';b='begin write(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.';
begin write(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.
Remark 1. In the case of a DOS implementation of Pascal the screen output of the program can look somewhat misleading, and it would be appropriate for that case to replace both "#10" in the program by "#13#10" and to insert a CR before the LF at the end of the first line.
Remark 2. The above program can be made even shorter, since both occurrences of ") end." in it can be replaced by ")end." (although this is not good for the legibility). A further shortening can be achieved by skipping both "#10" and writing the program on a single line instead on two ones. After all these changes the program will look as follows (the fact that it continues to be a quine was checked at least in the cases of Turbo Pascal for DOS and Free Pascal for Linux):
const a='const a=';b='begin write(a,#39,a,#39#59#98#61#39,b,#39#59,b)end.';begin write(a,#39,a,#39#59#98#61#39,b,#39#59,b)end.