Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can argc be zero on a POSIX system?

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?

like image 853
Sylvain Leroux Avatar asked Apr 13 '18 12:04

Sylvain Leroux


People also ask

Can argv be null?

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.

Is argc necessary?

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. ....

Is argc the default 1?

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.

What does argv 0 contain?

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.


2 Answers

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; } 
like image 35
Possum Avatar answered Oct 13 '22 05:10

Possum


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 of int and with no parameters:

int main(void) { /* ... */ }  

or with two parameters (referred to here as argc and argv, 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.

like image 116
dbush Avatar answered Oct 13 '22 07:10

dbush