### Question 20.34

Here's a good puzzle: how do you write a program which produces its own source code as its output?

It is actually quite difficult to write a self-reproducing program that is truly portable, due particularly to quoting and character set difficulties.

Here is a classic example (which is normally presented on one line, although it will ``fix'' itself the first time it's run):

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

(This program, like many of the genre, assumes that the double-quote character `"` has the value 34, as it does in ASCII.)

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback