Given the standard definition for the main program:
int main(int argc, char *argv[]) { ... }
Under which circumstances can argc
be zero on a POSIX system?
If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program start up. This means that, if argc is zero (and it can be), argv[0] is NULL.
Providing argc therefore isn't vital but is still useful. Amongst other things, it allows for quick checking that the correct number of arguments has been passed. 2 ... argc shall be the number of arguments passed to the program from the environment in which the program is run. ....
The argc parameter is an integer that holds the number of arguments on the command line. It will always be at least 1, because the name of the program is also counted. The argv parameter is a pointer to an array of character pointers.
By convention, argv[0] is the command with which the program is invoked. argv[1] is the first command-line argument. The last argument from the command line is argv[argc - 1] , and argv[argc] is always NULL.
To add to the other answers, there is nothing in C (POSIX or not) preventing main() from being called as a function within the program.
int main(int argc, int argv[]) { if (argc == 0) printf("Hey!\n"); else main(0,NULL); return 0; }
Yes, it is possible. If you call your program as follows:
execl("./myprog", NULL, (char *)NULL);
Or alternately:
char *args[] = { NULL }; execv("./myprog", args);
Then in "myprog", argc
will be 0.
The standard also specifically allows for a 0 argc
as noted in section 5.1.2.2.1 regarding program startup in a hosted environment:
1 The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type ofint
and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
2 If they are declared, the parameters to the
main
function shall obey the following constraints:
- The value of
argc
shall be nonnegative.argv[argc]
shall be a null pointer....
Note also that this means that if argc
is 0 then argv[0]
is guaranteed to be NULL. How printf
treats a NULL pointer when used as the argument to a %s
specifier is not spelled out in the standard however. Many implementations will output "(null)" in this case but it's not guaranteed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With