In the programmers’ lingo, quine is a program that outputs its own listing. The name quine comes from the american phylosopher William van Orman Quine.

Here is an example of quine in C:

char*f="char*f=%c%s%c;main() {printf(f,34,f,34,10);}%c"; main(){printf(f,34,f,34,10);}

To see it work on Linux, copy the text on a file quine.c, using a normal text editor. Then, compile the file with

   gcc quine.c

This will produce the output file


Finally, execute the file on a terminal with


If you prefer Windows, you must have a C compiler like Visual C/C++. You can find a free open source compiler here (don’t be scared by the “beta” version). After compiling, execute the program from a terminal.

Actually, the previous quine is quite straightforward. However, it is not trivial to invent new quines. There is a site deticated to computer quines in many languages.

Related to quines, there are obfuscated C programs.

The program in the figure, (which you can find here ), outputs a fractal, and the source code has the shape of a fractal!

There is “The International Obfuscated C Code Contest (IOCCC)”, a context about writing the most “incredible” C program (the one that gives the most unexpected output ). From the site’s archive, here is a fantastic implementation of the idea:

#define P(X)j=write(1,X,1)
#define C 39
int M[5000]={2},*u=M,N[5000],R=22,a[4],l[]={0,-1,C-1,-1},m[]={1,-C,-1,C},*b=N,
]=s;if(f){f=M[e=m[s=a[rand()/(1+2147483647/f)]]+g];j=j < f?f:j;f+=c&-16*!j;M[g]=
c|1<< s;m[*d++=e]=f|1<<(s+2)%4;}else e="d">b++?b[-1]:e;}P(" ");for(s=C;--s;P("_")
)P(" ");for(;P("\n"),R--;P("|"))for(e=C;e--;P("_ "+(*u++/8)%2))P("| "+(*u/4)%2

(click here to download file shapiro.c)

It looks like a random sequence of characters, doesn’t it? Actually, I think you will never be able to guess what will be the output of this program, until you compile and execute it!

The last little program I propose to you is the following one:

typedef struct n{int a:3,
b:29;struct n*c;}t;t*
v;}int i;h(u)t*u;{t*z,*v,*
w;int c,e;z=u->c,v=z->c,q(
char *sbrk();main(){t*e,*p,*o;
1);}}t*f(){return i||(i=1000,

(click here to download file august.c)

Warning: if you don’t stop this program (with ctrl-C), the program will coredump. Guess what comes out…

There are many other programs like these around. Unfortunately, many of them don’t compile anymore. Modern compilers don’t allow dirty tricks like the ones that are heavily used in such programs. However, it is still a lot of fun to see such oddities!


3 thoughts on “Quines

  1. Thanks for the pointers to obfuscated programs — good learning tool when teaching undergrad students about proper programming/ commenting practice. Do you have obfuscated Java programs as well?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s