March 29, 2010
A Quine in C
by Marco Vitanza

A Quine is a computer program which produces its own source code as the (only) output.

With the help of printf(), we can make a quine in 117 bytes of error- and warning-free C code (tested GCC 4.2).

#import<stdio.h>
main(){char*s="#import<stdio.h>%cmain(){char*s=%c%s%c;printf(s,10,34,s,34);}";printf(s,10,34,s,34);}

However, with a little trickery, we can reduce it down to just 74 bytes. The compiler will complain, but we really don't need the #include (GCC links against the C library automatically). We can also save 2 bytes by replacing char with int. Who needs types, right? Bytes are bytes.

main(){int*s="main(){int*s=%c%s%c;printf(s,34,s,34);}";printf(s,34,s,34);}

These are available as text files here: quine.c, and quine_nowarnings.c. If you want to try them yourself, make sure your text editor uses a single newline character (ASCII 10) for line endings.

$ gcc quine.c
(warnings)
$ ./a.out > tmp
$ diff tmp quine.c
$

Post Comment

Name:
Comment:  
Security Code: Verification Code Five Digits