A quine in computer science is a program which takes no input and produces a copy of its source code as its only output. There are "quine relays" or "ouroboros programs" which produce a chain of continuous files in different programming languages, eventually coming back to the original source code. For instance, a Java program may produce as its only output a C program, which, when run, produces as its only output the original Java program. A particularly egregious / beautiful example of this is mame's quine-relay, which currently consists of 128 different programming languages in a loop.
Long quine relays are easy enough to write, but in the spirit of code golf, what's the shortest possible quine in any language (esoteric or otherwise)? The following examples can be found at this codegolf.stackexchange thread. (Note that characters are considered to be 8-bit ASCII characters here.)
main(s), omitting the return type, is accepted (but sometimes gives a warning) by most C compilers, which simply look for a
main method and assume the return type is
int. This is an older (pre-1989) style C syntax which is still accepted by some compilers.
s is auto-casted by the C compiler to the necessary type, for the same reason as given above.
s equal to the given string.
printf(s=..., 34, s); first substitutes ASCII character #34 (
") to the
%c, so now the string to be printed is
The third argument passed to
s, the entire string, is now substituted into the
s was initialized at the beginning of the
printf statement, so it hasn't changed since. So the string to be printed now becomes
%1$c from the original
printf function call is resolved.
%1 gets the first argument (
$c formats it as a character. This is from a POSIX extension to the C-language. The final string then becomes (and is printed as):
...which is the original program! This example captures the spirit of quine programming.
Though this needs to be wrapped in
<script></script> tags to run in a browser, adding 17 bytes to the solution, it's still smaller than the C-language example shown above.
!, plus some variable
a, plus the string
(). In fact,
a is the name of a function, defined in the code above. When a function is printed like this, the source code of the function is simply printed for the user.
a() is called by placing the exclamation point (
!) before the function definition -- turning it into a function expression -- and the
() at the end, which results in the function being instantly called. These need to be added back to the beginning and end of the printed function to make this into a true quine.
UPDATE: (28 or 45 bytes)
It still needs to be wrapped in
<script></script> tags, but it requires 8 fewer characters than the previous solution.
Chicken is an esoteric programming language for which "chicken" is the only valid symbol. It was inspired by this 2002 paper and accompanying slides [*.ppt] by Doug Zongker, which was meant to satirize unintelligible conference presentations. (No video of the original presentation exists, though a second presentation of the slides by Zongker was captured on video in 2007.)
In this language, a single
chicken on a line encodes operation #1, which pushes the string
chicken onto the stack. A newline (or an empty line) encodes operation #0, which prints the top of the stack to the screen and exits. So the script
chicken executes op1, pushing a
chicken to the top of the stack, then the end of file (EOF) executes op0, which prints the top of the stack (
chicken) to the terminal. It's chickens all the way down.
GolfScript is a programming language written explicitly for code golfing, where a task is achieved in the shortest number of bytes possible. In GolfScript, when a number is written in the code anywhere, it is added to the top of the stack. Operations can be performed on the top two items in the stack. At the end of a GolfScript program, the contents of the stack are printed to the screen, followed by a newline character,
\n. So the above code adds a
1 to the top of the stack, then prints it, and appends a
\n (hence the necessary newline at the end, doubling the byte count).
See also: 7, Applescript, memes, Seriously, Y
HQ9+ is an esoteric programming language with only four commands:
- H: prints "Hello, world!"
- Q: prints the source code of the program (a quine)
- 9: prints the lyrics of the song "99 bottles of beer"
- +: increments an inaccessible accumulator (essentially does nothing)
Stretching the definition of a programming language (HQ9+ is not Turing complete), this language nevertheless gets its quine down to a single byte. At this point, it's very difficult to tell which languages can create "true" quines, which encode some command, rather than "false" quines, which simply print their content to the terminal as a default action, without actually encoding for some other command (for instance, pushing characters to the stack, etc.).
Tied with: Arcyóu, Burlesque, CJam, J, TeaScript, TI-BASIC
Famously stretching the definition of a quine was this 1994 entry to the International Obfuscated C Code Contest (IOCCC), consisting of an empty file named smr.c. "While strictly speaking, smr.c is not a valid C program, it is not an invalid C program either", write the judges, issuing a disclaimer that future rules will specify a minimum file size of 1 character. (Note: most modern C compilers will complain about an "undefined reference to `
main' when given this code.)
So there you have it: "true" quines probably start becoming "false" quines around 1 or 2 characters, and as there are now many, many esoteric programming languages devised specifically for code golf, there are now many, many 1- and 2-character quines available. Go forth and golf!
This post originally appeared in a slightly different form on my (now defunct) Wordpress blog.