A Simple Quine


A "quine" is a computer program that accepts no input and produces a copy of its own source code [Wikipedia.org]. If one searches for quines on the Internet, one will find all kinds of variations written in various languages. This blog post presents a simple C++11 quine.

Substituting Strings Within Strings

Many short quines one can find on the Internet rely on the ability to substitute strings within other strings. For example consider the following BASH shell script's use of string substitution to output, "Hello World!" [this script is not a quine]:

S="Hello $S\!"
echo $S

Since many languages (e.g., C/C++) are 100% statically typed and don't permit such string substitutions at compile-time, one would not write a quine this way in these languages. This post will present one using C++11 which could easily be ported to other languages with similar restrictions.

The presented code's emphasis is on its simplicity, i.e., the code is meant to be easy-to-read and easy to figure out how it works. Unfortunately the syntax highlighter does not properly highlight the code –so I enabled line numbering and refer to specific line numbers.

A Simple C++11 Quine

Here is a simple C++11 quine (with broken syntax highlighting since it doesn't handle raw string literals):

#include <string>
#include <iostream>

auto const data = R"data(
auto const preamble_begin =
#include <string>\n#include <iostream>\n\n"
  "auto const data = R\"data(";

auto const preamble_end = ")data\";\n";

auto const program = std::string{} + preamble_begin + data
  + preamble_end + data;

int main()
  using namespace std;
  cout << program << endl;

auto const preamble_begin =
#include <string>\n#include <iostream>\n\n"
  "auto const data = R\"data(";

auto const preamble_end = ")data\";\n";

auto const program = std::string{} + preamble_begin + data
  + preamble_end + data;

int main()
  using namespace std;
  cout << program << endl;

With the exception of the program variable, all of the other variables are of type const char * const, i.e., they are compile-time constants. I used raw strings to avoid additional escaping that would be needed and to span the string across multiple lines –which makes the code much more readable.

This is an example of a C++11 raw string literal:

except the closing delimiter:)delimiter"

where "delimiter" is a possibly empty token. This means one can copy-and-paste string literals inside of other string literals when they have different delimiters. Without this ability, one would have to use traditional C/C++ string escaping. Except for simple cases, traditional escaping techniques will hurt quine code readability as well as the ease that one can write the quine.

Notice that the "data" raw string literal starting on line 4 with R"data( ends on line 19 with )data". Clearly, the use of the raw string enabled me to copy-and-paste a large part of the program source code into the "data" raw string literal. This makes coding a quine so much easier! (I kept the rest of the string literals as traditional C/C++ strings since they are short and easy-to-read.)

The quine works by computing the program variable's value (i.e., lines 27 and 28) at run-time and outputting it (i.e., line 33). Within the source code, the entire program effectively appears twice, however, in the executable binary the source code effectively appears once (broken up) since the necessary concatenations reconstructing the original program will occur at run-time.

The reason this program can output itself is due to how it is broken up: no variable tries to quote itself. Instead the program is broken up across a number of variables (most of which are compile-time constant literals) and a final variable, i.e., program, is used to generate the program so it can be output.

Closing Comments

The presented code can be made more interesting, if it output a program that computed the next iteration of something (if possible) or itself (if not possible). For example, one could have a literal array of std::size_t which holding the first numbers in the fibonacci sequence:

std::size_t const fib[] = { 1, 1, 2, 3, 5 };

and upon running the program, it computes the next number in the sequence. If the next number overflows std::size_t, then the program would output itself, otherwise, the program would output itself with the fib array updated to have one more element in it.

Another variation would involve having a program actually output new versions of itself, compile the new code, and then exec() the new executable. Obviously care has to be taken to ensure that the program terminates and doesn't use up all of the CPU.

An easy "back-door" debugging hack to help ensure that the program-that-runs-another-program shuts down when desired/debugging would be to check for the existence of a special file (and if it exists, it terminates instead of calling exec()).

Finally, consider not having self-bootstrapping programs be quines or quine-capable by writing and using a static/dynamically-linked library of code common to all generated programs to keep the generated program code short.

One Reply to “A Simple Quine”

Leave a Reply

Your email address will not be published. Required fields are marked *