Quines: Programs that Output Their Own Source Code

A fun computer science puzzle to challenge your ingenuity

Don Cross
5 min readNov 12, 2019
Photo by JOSHUA COLEMAN on Unsplash

Back in 1991, I was riding in a van with some fellow university students, on our way back from a computer programming competition. We were in a good mood because we had just won first place in the southeastern United States. We were exhausted and amped up at the same time.

Along with reciting Monte Python quotes, eating junk food, and inflicting horrible singing on each other, we also spent time discussing algorithms. One topic came up that intrigued me: is it possible to write a program that prints its own source code?

We agreed on some rules. The program is not allowed to read its source code from an external source. You are not allowed to make up a new programming language to solve the problem; you must use a pre-existing programming language.

A solution requires source code that, when compiled and executed, generates identical source code as output. Your code may use library routines supplied with the language, and it does not need to print their source code (which might not be available anyway).

It turns out this wasn’t an original idea. Later we learned that a program that prints out its own source code is called a quine, in honor of the logician Willard

--

--