self-printing C programs

utzoo!decvax!harpo!ihps3!ihuxr!lew utzoo!decvax!harpo!ihps3!ihuxr!lew
Wed Nov 3 11:16:47 AEST 1982


Steve Wagar's self-printing program:

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

is far from working. Here is my best effort at rescuing it:

main(){char*a="main(){char*a= %s%c%s %s %s%c%s%c ;a[14]=0;a[33]=0;printf(a+15,a,34,a,a+15,a+34,34,a+34,10);}";a[14]=0;a[33]=0;printf(a+15,a,34,a,a+15,a+34,34,a+34,10);}

(That's a 168 character line, I hope it goes through the net OK.)
Can anybody do better? I think the self-printing problem is very deep.
I have seen in print the statement that a self-printing program can
be written in any general programming language, but I don't think
it's true. What if C didn't allow decimal specification of characters?
I think you could easily have a language in which you could write
a program to produce any given output, but not a self-printing program.

I think the challenge to write a self-printing program in a canonical
C style is non-trivial. I propose a contest for the shortest self-printing C
program which is unaltered by cb.

Lew Mammel, Jr. ihuxr!lew



More information about the Comp.lang.c mailing list