Smallest self-replicating program

A quine is a computer program that outputs its own source code. An entrant in the 1994 International Obfuscated C Code Contest created the smallest quine possible.

Computer programming has a rich history of esoteric humour. Take, for example, GNU. This operating system mimics the functions of Unix. Its name is an acronym for “GNU’s Not Unix.” So the first letter in GNU stands for… GNU. It’s a recursive acronym; the first letter of the acronym is the full acronym, and the first letter of that acronym is the full acronym, and on and on into infinity.

Programmers, I am told, find this hilarious. Maybe because so much programming involves loops and ridiculous acronyms, and this is a way to combine those two?

Similar to the recursive acronym is the quine. A quine is a computer program that is designed to do one thing: output an exact copy of itself and nothing else. A quine is completely useless, of course; it’s just an exercise in creative programming. And it is much more challenging than you’d think.

Different programming languages have different ways to achieve the quine. It’s pretty easy in JavaScript (one of the few languages I know well enough to parse). You define a function that outputs an alert that displays that function:

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

The smallest possible quine was an entry in the International Obfuscated C Code Contest (IOCCC). This is a semi-joke semi-serious contest to produce creative and bizarre code in the C programming language; it has been running on and off since 1984. The competition encourages entrants to find creative ways to “hack” the contest’s own rules, and the quine entry certainly did that.

This is the shortest quine:

No, you’re not seeing things. It’s a blank line; precisely zero; absolutely nothing. If you run this program, the output is… well, it’s nothing. So, technically, it works. The program outputs itself, therefore it is a quine. The programmer, Szymon Rusinkiewicz from Stanford, won an award for “worst abuse of the rules.” In the IOCCC, this is a high honour.

(End note: both the recursive acronym and the quine were first defined in the seminal text Gödel, Escher, Bach: an Eternal Golden Braid. It’s a fun, mind-bending read.)

Leave a Reply