DEV Community

Cover image for World's Smallest Quine, "Guaranteed"
Andrew (he/him)
Andrew (he/him)

Posted on

World's Smallest Quine, "Guaranteed"

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.)

C (60 bytes)

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
Enter fullscreen mode Exit fullscreen mode

Try it here!

Explanation:

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="main(s){printf(s=%c%s%1$c,34,s);}" sets 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 main(s){printf(s="%s%1$c,34,s);}.

The third argument passed to printf, s, the entire string, is now substituted into the %s. s was initialized at the beginning of the printf statement, so it hasn't changed since. So the string to be printed now becomes main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}%1$c,34,s);}.

Finally, the %1$c from the original printf function call is resolved. %1 gets the first argument (34) and $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):

main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
Enter fullscreen mode Exit fullscreen mode

...which is the original program! This example captures the spirit of quine programming.

JavaScript (36 or 53 bytes)

!function a(){alert("!"+a+"()")}()
Enter fullscreen mode Exit fullscreen mode

Try it here!

Explanation:

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.

The alert() function in JavaScript sends an alert dialog box to the user. In this case, the alert contains the character !, 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)

dev.to user prplz suggested a shorter JavaScript quine using ES6:

(z=x=>alert(`(z=${z})()`))()
Enter fullscreen mode Exit fullscreen mode

It still needs to be wrapped in <script></script> tags, but it requires 8 fewer characters than the previous solution.

Chicken (7 bytes)

chicken
Enter fullscreen mode Exit fullscreen mode

Try it here!

Explanation:

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 (2 bytes)

1

Enter fullscreen mode Exit fullscreen mode

Try it here!

Explanation:

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+ (1 byte)

Q
Enter fullscreen mode Exit fullscreen mode

Try it here!

Explanation:

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

C, again (0 bytes)


Enter fullscreen mode Exit fullscreen mode

Explanation:

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.

Top comments (9)

Collapse
 
cwreacejr profile image
Charles Reace

PHP in 26 bytes:

<?php readfile(__FILE__);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
cwreacejr profile image
Charles Reace

PS: Depending on your PHP configuration, you could shorten it by 3 bytes to...

<? readfile(__FILE__);
Collapse
 
siddharthshyniben profile image
Siddharth

TBH this is not a "real" quine as a quine cant read its own source.

Collapse
 
awwsmm profile image
Andrew (he/him)

Cool! How does this work, though? I'm not very familiar with PHP.

Collapse
 
cwreacejr profile image
Charles Reace

readfile() reads the contents of the specified file and sends them directly to the output buffer.

__FILE__ is a built-in PHP "magic" constant that contains the full path to the current source code file. (If you're inside an include file at that point, then it will be that file, not the "main" file that included it.)

Collapse
 
rohitshetty profile image
Rohit Shetty • Edited

quine in bash, Although its quite a cheating.
7 bytes.

cat $0
Collapse
 
cwreacejr profile image
Charles Reace

Heh...you just made my PHP quine shorter. :)

<? echo $0

(And I remembered that the ; is a command separator, not a terminator, so saved another byte.)

Collapse
 
awwsmm profile image
Andrew (he/him)

That is better! I'll update the post.

Collapse
 
joao9aulo profile image
João Paulo Martins Silva

Very cool, i find quines and self replicating programs a fascinating matter. Looking foward for more posts like these!