Quine Programs

Quines are cool. You should learn to write them. They make you think about levels of meaning—about values and their representations.

Introduction

A quine program, or quine, is a program that outputs its own source code when run. A quine is not allowed to “step outside itself” by, for example, printing out the contents of the file in which it is contained or using introspective capabilities to print its own representation. Instead, it must compute its own source code.

The Basic Idea

The classic way to produce a self-printing program has two steps:

  1. Initialize a string variable, with a placeholder for interpolation.
  2. Print the string, interpolating the string into itself.

The trick is getting the string just right. How you do this varies from language to language depending on how variables are declared, the method of interpolation, the possible need for semicolons and newlines, the kinds of quotes required, and so on. Dealing with quotes is interesting; you need to find a way to specify that a quote character is to be printed without actually using a quote character.

Examples

Note: In each of the following examples, the programs are assumed to have no newline at the end of the program text unless specified.

Bash

Like most things in Bash, this one uses an interesting quirk. Although single quotes are the strong quotes, expanding nothing, Bash's printf does interpret octal escapes as the character with the specified codepoint. Go figure.

s='s=\47%s\47;printf "$s" "$s"';printf "$s" "$s"

Ruby

Ruby's printf doesn't have the Bash quirk of expanding octal escapes within single-quoted strings, but it has something better. The %p format specifier automatically adds double quotes around the string being interpolated.

s="s=%p;printf s,s";printf s,s

Ruby also has the % operator on strings to do the formatting, so you can use the plain print instead of printf and save one character.

s="s=%p;print s%%s";print s%s

If you use puts instead of print then you will need a newline at the end of your source code file.

s="s=%p;puts s%%s";puts s%s
 

If you didn't know about the %p format specifier, then you use the fact that the double quote character has codepoint 34.

s="s=%c%s%c;printf s,34,s,34";printf s,34,s,34

Python

In Python, the %r format specifier automatically supplies single quotes. It is simplest for the code to end with a newline character.

s='s=%r;print(s%%s)';print(s%s)
 

If you want the code without the newline, you need to write:

s='s=%r;print(s%%s,sep="")';print(s%s,sep="")

Lua

Lua has the %q format specifier which adds the double quotes to interpolated strings. It doesn't have printf (at least not as of Lua 5.3), but you can use the format method on strings.

s="s=%qprint(s:format(s))"print(s:format(s))

Perl

I don't think there is any special format specifier for Perl's printf that adds the quotes directly, but we can do it the classic way with the codepoints. We have to use single quotes here, otherwise the variables will automatically get interpolated.

$s='$s=%c%s%c;printf($s,39,$s,39);';printf($s,39,$s,39);

There actually is another really cool way to get the quotes in Perl: you can use q for quoting.

$s=q($s=q(%s);printf($s,$s););printf($s,$s);

JavaScript

All of these JavaScript quines run under node.js. They use console.log, which always appends a newline, so you will need a newline at the end of each script.

s="s=%j;console.log(s,s)";console.log(s,s)
 

This is an interesting one, too. It appears to be right on the borderline of cheating. It doesn't print its entire representation, but it does rely on the the fact that functions have a particular representation. I found it (well, something really close to it) on the Rosetta Code page of quines.

(function a(){console.log('('+a+')()')})()
 

This one is really close to cheating, because it uses eval. It's also based on an entry on the Rosetta Code page of quines.

code='var q=unescape("%27");console.log("code="+q+code+q+";eval(code)")';eval(code)
 

CoffeeScript

This quine is just like one of the JavaScript examples except we don't need the parens in the calls. This one runs under node.js and requires a newline at the end of the script.

s="s=%j;console.log s,s";console.log s,s
 

C

Old-style (K&R)

This is the classic. I don't remember where I first found it. I wrote it in the margin of a book sometime in the 1980s.

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

C99

Modern C needs should have the #include directive, which means the code needs a newline. Here it is with a few additional newlines, just to make the code be not so wide. There's no new line at the very end, though.

#include <stdio.h>
int main(){
char*s="#include <stdio.h>%cint main(){%cchar*s=%c%s%c;%cprintf(s,10,10,34,s,34,10);return 0;}";
printf(s,10,10,34,s,34,10);return 0;}

Of course, to get away with as much as possible, while making the code legal (although nasty), we can just declare printf ourselves and omit the return 0. The gcc compiler will accept and run the program.

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

Java

This is a direct translation of the classic K&R quine.

class Q{public static void main(String[]a){String s="class Q{public static void main(String[]a){String s=%c%s%c;System.out.printf(s,34,s,34);}}";System.out.printf(s,34,s,34);}}

I found a slightly shorter version on the Rosetta Code quine page, making use of the positional specifier in printf.

class Q{public static void main(String[]a){String s="class Q{public static void main(String[]a){String s=%c%s%1$c;System.out.printf(s,34,s);}}";System.out.printf(s,34,s);}}

Scala

This one is simple and direct, based right off the old K&R C quine, and using the positional specifier in printf.

val s="val s=%c%s%1$c;printf(s,34,s)";printf(s,34,s)

Clojure

Here is the translation of the classic version, using the positional specifier in printf.

(let [s "(let [s %c%s%1$c] (printf s 34 s))"] (printf s 34 s))

The version that appears in several places on the web, including the Rosetta Code quine page, is a little different: it doesn't even write to standard output. It's just an expression whose printed form looks like the code.

((fn [x] (list x (list (quote quote) x))) (quote (fn [x] (list x (list (quote quote) x)))))

Rust

Rust has a way to quote an argument in its formatted print macro. However, this macro requires the first argument to the print macro be a string literal! So the best thing to do is make use of the positional argument specifier:

fn main(){print!("fn main(){{print!({0:?},{0:?})}}","fn main(){{print!({0:?},{0:?})}}")}

HQ9+

No page on quines is complete without an entry written in HQ9+.

Q