World's Smallest Quine, "Guaranteed"
Tags: quines, c, javascript, codegolf
Published: 2018-10-15 23:06:41

A [quine](https://en.wikipedia.org/wiki/Quine_(computing)) 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](https://github.com/mame/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](https://codegolf.stackexchange.com/), what's the shortest possible quine in any language (esoteric or otherwise)? The following examples can be found at [this codegolf.stackexchange thread](https://codegolf.stackexchange.com/questions/69/golf-you-a-quine-for-great-good). (Note that characters are considered to be 8-bit ASCII characters here.)

# C (60 bytes)
```cpp
main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
```

[Try it here!](https://onlinegdb.com/BySo4TV3G)

## 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](https://stackoverflow.com/questions/1585390/c-function-syntax-parameter-types-declared-after-parameter-list) 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](https://stackoverflow.com/questions/19327441/gcc-dollar-sign-in-printf-format-string). The final string then becomes (and is printed as):

```cpp
main(s){printf(s="main(s){printf(s=%c%s%1$c,34,s);}",34,s);}
```

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

# JavaScript (36 or 53 bytes)

```javascript
!function a(){alert("!"+a+"()")}()
```

[Try it here!](http://js.do/code/209963)

## 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](https://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function) -- 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](https://dev.to/prplz) suggested a shorter JavaScript quine using [ES6](http://es6-features.org):

```javascript
(z=x=>alert(`(z=${z})()`))()
```

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

# Chicken (7 bytes)

```
chicken
```

[Try it here!](http://torso.me/chicken)

## Explanation:

[Chicken is an esoteric programming language for which "chicken" is the only valid symbol](http://esolangs.org/wiki/Chicken). It was inspired by [this 2002 paper](https://isotropic.org/papers/chicken.pdf) and [accompanying slides [*.ppt]](http://isotropic.org/papers/chicken.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](https://www.wired.com/2007/05/post_1/).)

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
 
```

[Try it here!](https://copy.sh/golfscript/?c=MQo=)

## Explanation:

[GolfScript](http://www.golfscript.com/golfscript/tutorial.html) 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
```

[Try it here!](https://almnet.de/esolang/hq9plus.php)

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

```cpp
 
```

## 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](http://www0.us.ioccc.org/1994/smr.hint) 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._