Quines

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

a.out

Finally, execute the file on a terminal with

./a.out

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,
*d=N,c,e,f,g,i,j,k,s;main(){for(M[i=C*R-1]=24;f|d>=b;){c=M[g=i];i=e;for(s=f=0;
s<4;s++)if((k=m[s]+g)>=0&&k=16!=M[k]>=16))a[f++
]=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*
f();r(){}m(u)t*u;{t*w,*z;
z=u->c,q(z),u->b=z->b*10,
w=u->c=f(),w->a=1,w->c=z->
c;}t*k;g(u)t*u;{t*z,*v,*p,
*x;z=u->c,q(z),u->b=z->b,v
=z->c,z->a=2,x=z->c=f(),x
->a=3,x->b=2,p=x->c=f(),p
->c=f(),p->c->a=1,p->c->c=
v;}int i;h(u)t*u;{t*z,*v,*
w;int c,e;z=u->c,v=z->c,q(
v),c=u->b,e=v->b,u->b=z->b
,z->a=3,z->b=c+1,e+9>=c&&(
q(z),e=z->b,u->b+=e/c,w=f(
),w->b=e%c,w->c=z->c,u->c=
w);}int(*y[4])()={r,m,g,h};
char *sbrk();main(){t*e,*p,*o;
o=f(),o->c=o,o->b=1,e=f(),
e->a=2,p=e->c=f(),p->b=2,
p->c=o,q(e),e=e->c,(void)write
(1,"2.",2);for(;;e=e->c){q(e),
e->b=write(1,&e->b["0123456789"],
1);}}t*f(){return i||(i=1000,
k=(t*)sbrk(i*sizeof(t))),k+--i;
}q(p)t*p;{(*y[p->a])(p);}

(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!

Advertisements

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?

    Reply

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