[comp.lang.c] Silly programming problems

maart@cs.vu.nl (Maarten Litmaath) (03/18/89)

frisk@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@cs.vu.nl, mcvax!botter!maart

bph@buengc.BU.EDU (Blair P. Houghton) (03/22/89)

In article <2179@fireball.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
>frisk@rhi.hi.is (Fridrik Skulason) writes:
>\	   "write a one line C program that produces the source code
>\	   to itself when run."
[...]

"the source code to itself"  -- that's what it sez.

>A cheat.
>----------8<----------8<----------8<----------8<----------8<----------
>main(){execlp("cat","",__FILE__,(char*)0);}
>----------8<----------8<----------8<----------8<----------8<----------

Not so big a cheat.  It seems it really would produce it's own source,
rather than just a copy or whatever its buggy little self happens to
contain.  This way, you are _certain_ that the program is what its
source looks like.  Except now you have to keep them together
forever...but it may be the only answer that fits the spec perfectly.

				--Blair
				  "Maybe there should be some
				   'find' and 'diff' action in
				   there, just to be sure..."