[comp.lang.misc] Programs which print themselves out

lui@CS.UCLA.EDU (11/05/87)

What is the shortest program, in some popular language like C or Pascal, which
prints itself out when executed?

	Stephen Lui
	UCLA Department of Computer Science and

	Physical Address:
	Hughes Aircraft Company
	Radar Systems Group
	Centinela Ave. & Teale St.
	Culver City, CA.

	(213) 305-2085

	Mailing Address:
	Hughes Aircraft Company
	M/S RC R49 2563
	P.O. Box 92426
	Los Angeles, CA
	90009-2426

	ARPA:  lui@cs.ucla.edu
	UUCP:  ...!{cepu,ihnp4,trwspp,ucbvax}!ucla-cs!lui

oster@dewey.soe.berkeley.edu (David Phillip Oster) (11/06/87)

In article <9015@shemp.UCLA.EDU> lui@CS.UCLA.EDU (Stephen Lui) writes:
>What is the shortest program, in some popular language like C or Pascal, which
>prints itself out when executed?

Starting with an arbitrary text file (such as this article), named "junk"
the sequence:

% mv junk junk.c
% cc junk.c >& junk

repeated 5 or 6 times quickly converges to a file which, when compiled
generates itself as an error message.

For any language, the null program, when run, generates itself as output.

In Apl, or Lisp, the next largest answer is of course the program:

1

this program, when run, produces itself as a value.

--- David Phillip Oster            --A Sun 3/60 makes a poor Macintosh II.
Arpa: oster@dewey.soe.berkeley.edu --A Macintosh II makes a poor Sun 3/60.
Uucp: {uwvax,decvax,ihnp4}!ucbvax!oster%dewey.soe.berkeley.edu

chris@cooper.cooper.EDU (Chris Lent ) (11/08/87)

In article <9015@shemp.UCLA.EDU>, lui@CS.UCLA.EDU writes:
> What is the shortest program, in some popular language like C or Pascal,
> which prints itself out when executed?
> 
> 	Stephen Lui
> 	UCLA Department of Computer Science
Hi folks,
	Though this is probably NOT what Stephen was thinking about I
thought the net might find this amusing. Since the Bourne shell is
also a popular programming language, here are two Shell/C programs:

	selfcat.c :  Type selfcat.c and this program will cat itself
	selfhello.c: Type selfhello.c and this program will compile
			itself into an executable called a.out

These programs use the fact that the pound (or sharp) sign, '#',
means something different to the Bourne shell and the 'C' pre-
processor.  In the case of the Bourne shell '#' is a comment,
so the following lines are just comments:
	#ifdef NEVERDEF
	#undef NEVERDEF
	#endif
	#ifdef NEVERDEF
	#Bourne Shell commands follow:

BUT to the 'C' pre-processor they mean:
	if the symbol NEVERDEF is defined (by a command line -D perhaps)
	remove the definition of NEVERDEF.
	Then the following ifdef'd section will never be compiled
	into the program.

So in that ifdef'd section we can safely put a series of Bourne
shell commands. At the end of the section we put an exit to
quit the series of shell commands.

Note that the pound sign followed by a comment is ignored
by the 'C' pre-processor, but a normal Bourne Shell comment
like:	#hello
will give error messages from the pre-processor when compiled
in a 'C' program.

It's a cute trick to amaze your friends.

Enjoy,
Chris Lent
cmcl2!cooper.cooper.edu!chris
(203) 452-1522
#--------------------------CUT HERE-----------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	selfcat.c
#	selfhello.c
# This archive created: Sat Nov  7 21:12:48 1987
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'selfcat.c'
then
	echo shar: "will not over-write existing file 'selfcat.c'"
else
cat << \SHAR_EOF > 'selfcat.c'
#ifdef NEVERDEF
#undef NEVERDEF
#endif
#ifdef NEVERDEF
# /* Bourne Shell commands follow: */
cat $0
exit
# /* End of Bourne Shell commands */
#endif
main()
{
	printf("Gee, this program cat's itself!\n");
}
SHAR_EOF
chmod +x 'selfcat.c'
fi
if test -f 'selfhello.c'
then
	echo shar: "will not over-write existing file 'selfhello.c'"
else
cat << \SHAR_EOF > 'selfhello.c'
#ifdef NEVERDEF
#undef NEVERDEF
#endif
#ifdef NEVERDEF
# /* Bourne Shell commands follow: */
cc $0
exit
# /* End of Bourne Shell commands */
#endif
main()
{
	printf("Hello, world!\n");
}
SHAR_EOF
chmod +x 'selfhello.c'
fi
exit 0
#	End of shell archive

mac3n@babbage.UUCP (11/11/87)

We go through this every few years.  My personal favorite is the empty program.
(See below)