Self-printing C Programs

utzoo!decvax!harpo!floyd!cmcl2!mcbryan utzoo!decvax!harpo!floyd!cmcl2!mcbryan
Wed Nov 3 21:44:37 AEST 1982

As I understand it C has no I/O.   Thus writing a self-reproducing C
program is meaningless.   Given a particular library of C I/O routines
one could of course do it (e.g. with STDIO ).   However someones else's
IO library might allow shorter self-replications.   For example, the
OMCBIO library has a routine barf() which prints the source file
containing a reference to it.   Thus the program:

#include <omcbio.h>


is self-reproducing.  

The above program actually compiles and runs correctly on my system (4.1 bsd).
It must be run in the directory where it is compiled.

The file  omcbio.h  contains the single line:

#define  barf()   execlp("/bin/cat","cat",__FILE__,0)

This has the nice feature of self-anihalation (overlay) immediatly after the
An obvious extension using fork() provides a tree of self-replicating

More information about the Comp.lang.c mailing list