Silly programming problems

Maarten Litmaath maart at cs.vu.nl
Sat Mar 18 02:57:38 AEST 1989


frisk at rhi.hi.is (Fridrik Skulason) writes:
\	   "write a one line C program that produces the source code
\	   to itself when run."

Spoilers:

Actually a 2 liner (doesn't exceed 80 character limit):
----------8<----------8<----------8<----------8<----------8<----------
char*p="char*p=%c%s%c%c;main(){printf(p,34,p,34,10,10);}%c"
;main(){printf(p,34,p,34,10,10);}
----------8<----------8<----------8<----------8<----------8<----------

A true 1 liner (join the 2 lines):
----------8<----------8<----------8<----------8<----------8<----------
char*p="char*p=%c%s%c;main(){printf(p,34,p,34,10);}%c";main(){printf(p,34,p,34,
10);}
----------8<----------8<----------8<----------8<----------8<----------

The program below won't print a terminating newline, so the source shouldn't
contain '\n'. (Is this OK with ANSI?)
----------8<----------8<----------8<----------8<----------8<----------
char*p="char*p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
----------8<----------8<----------8<----------8<----------8<----------

A cheat.
----------8<----------8<----------8<----------8<----------8<----------
main(){execlp("cat","",__FILE__,(char*)0);}
----------8<----------8<----------8<----------8<----------8<----------
-- 
 Those against Rushdie haven't          |Maarten Litmaath @ VU Amsterdam:
   learned anything from the Dark Ages. |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.lang.c mailing list