When is argc = 0 useful?
Roger House
roger at everexn.com
Sat Nov 17 10:04:20 AEST 1990
While specifying code for implementing a C runtime function which sets
up argc and argv for main, a question arose as to when argc might be
zero. In section 2.1.2.2.1 (Program Startup) of the ANSI C reference it
says that argc must always be nonnegative and argv[argc] must be a null
pointer. Thus, if argc = 0, then argv[argc] = null pointer. However,
in the environment that I am working in, the program name is always
known, so when there are no parameters to the program on the command
line, argc = 1, argv[0] = progam name, and argv[1] = null. Thus, argc
is never zero when main is called.
The only situation I can think of in which argc might be zero is in an
environment in which the program name is not known. Then, if there are
no parameters on the command line, argc = 0 and argv[0] = null might be
useful. But, on the other hand, the standard already takes care of the
situation when the program name is not known. In this situation, if
argc > 0 then argv[0][0] must be a null character.
Thus, a portable program must treat the following two situations in the
same way:
1) argc = 0
2) argc = 1 and argv[0][0] = 0
IMHO, it would be simpler and cleaner to simply require argc > 0 in all
instances. Comments, anyone?
Roger House
More information about the Comp.std.c
mailing list