Index Home About Blog
Newsgroups: comp.lang.c
From: dmr@research.bell-labs.com (Dennis Ritchie <7549-15328> 0112710)
Subject: Re: Hey! When did argv become null terminated? Have I been clueless 
	all these years?
Date: Tue, 15 Oct 1996 02:47:13 GMT

Some of the history:

Through Unix Sixth Edition (1975) argv[argc] was filled
in with the value -1, in part to help detect accesses
through it, and thus to encourage use of argc instead of
depending on a sentinel in argv.  By the Seventh Edition
(1979) and in the soon-following 32V and BSD versions,
argv[argc] was set to NULL by the operating system.
This was documented in the Unix manuals (under exec(2))
and thus implicitly for C in the Unix evironment.

No complete discussion of the calling sequence for main
appeared in the accompanying C manuals nor in K&R I;
the White Book (1st edition) has a brief discussion on
p. 110, but there's nothing in the reference section.
This should be regarded as a lacuna, though it might
also be justified as an attempt to separate the language
description from an implementation for a particular
environment.

ANSI and then ISO did make the language rules for
main clearer.  I suspect the committee could
have gone further in cleaning up the situation
simply by saying that that the prototype for
main in a hosted environment is

 int main(int, char **);

that it is called in a context equivalent to

	exit(main(argc, argv));

and that in situations where the environment is
otherwise 'hosted' (i.e. with a full set of headers
and libraries available) but there is no notion of
command line, then argc is 0.  Being a bit more
definite here would have simplified questions about what
happens when main is called recursively and similar
details that sometimes trip people up.

	Dennis



Index Home About Blog