I almost hate to bring up a sentence in paragraph 2 §5.10: "By convention,
argv[0] is the name by which the program was invoked, so argc is at least
1." Doesn't that say something about the relevance of K&R to the argc==0
thread above?
Sure, but note that on Unix - where C was first used - that
convention is subject to the whim of the programmer who executes the
program in the first place. (In fact, this is a common mistake made
by new Unix programmers - they invoke a program with no arguments at
all because they assume the environment will provide argv[0] for
them.)
Obviously, Dennis Ritchie knew perfectly well how the execv system
call worked in Unix when he co-wrote K&R, so he knew that programs
couldn't depend on argv[0] being set. But the C / Unix mindset of
the 1970s was largely a "best effort" one: let's get some work done,
and if we have to assume some error conditions are sufficiently rare
that we'll let the program crash if they happen, so be it.
(Ritchie's comment on the SIGPIPE signal is a good example: SIGPIPE
was invented so that pipelines would work correctly even if programs
didn't check for error returns when writing to standard output.)
So indeed it's just that: a convention, and the truly robust program
should assume that some rude invoker might violate it. The quick-
and-dirty program, tossed together for a single use, probably needn't
bother. (Though most programmers have seen at least a few of those
quick-and-dirty, temporary programs get distributed and come back to
plague them later...)
--
Michael Wojcik (e-mail address removed)
"Well, we're not getting a girl," said Marilla, as if poisoning wells were
a purely feminine accomplishment and not to be dreaded in the case of a boy.
-- L. M. Montgomery, _Anne of Green Gables_