Photo by Tine Ivanič on Unsplash

Build a self replicating program (quine)

A simple introduction to the world of computer viruses

Jean-Baptiste Terrazzoni
42 Stories
Published in
2 min readAug 26, 2019

--

The rules are simple, build a program that outputs its source code. Opening and reading the source file is considered cheating. More precisely, a quine receiving any kind of input is considered invalid.

The Kleene’s theorem

This exercise is a direct application of the Kleene’s theorem. But the theorem was created long before computers. Here is how I understand it.

First form — The fixed-point theorem

It says that for every conditional program e, there is a program true for ϕe(x) = f(e,x) for any input x. e is the program logic, and ϕe its syntax. It means that there is one program which execution will result in its source code.

Second form

From this we can deduct its second form, which states that for every conditional program, there is one program which execution will result in the same program.

Let’s build one

Let’s build your first simple quine. You have to print the source code of your program without opening it, use at least 2 comments and two functions. A diff <(./quine) quine.c should return nothing.

Solution in C

Good to know

This type of programs was named Quine in honor of the philosopher Willard Van Orman Quine, who worked on the subject of indirect self-reference.

Want more ?

I worked on many of these exercices during my journey at the 42 Paris School. If you like these challenges, try implementing the other quines available on my repository 😊

I’m starting a new website called myopen.market. It’s still in a early stage, but if you found this article useful, subscribing to its newletters would be the best way to encourage me ❤️

--

--

Jean-Baptiste Terrazzoni
42 Stories

Software developer interested in finance and crypto. Seeking new projects and ideas. Connect with me! pexels.com/@jterrazz