Self-reproducing C programs

Tom Duff td at alice.UUCP
Wed Sep 19 07:12:06 AEST 1984


It is possible to generate (syntactically invalid) C programs whose error message
output from cc is identical to the program input.  An easy way is to use the
following shell script:

echo hi >x
echo bye >junk.c
until cmp -s x junk.c
do	mv x junk.c
	cc junk.c 2>x
done

When this terminates, it will have found a fixed-point of the cc command.
I won't reproduce the output here.  It varies from compiler to compiler,
and can be more than a hundred lines long.

Thanks to Dennis Ritchie and Rob Pike for suggesting this approach.



More information about the Comp.lang.c mailing list