What I have understand about passing arguments to main() from command line is that argc has a minimum value of 1 and argv[0] will always have the program name with its path in it.
If arguments are provided at the command line, then argc will have a value greater than one and argv[1] to argv[argc-1] will have those arguments.
Now a paragraph at this link says that
argv[0]will be a string containing the program’s name or a null string if that is not available.
Now, how and when can argv[0] have null string? I mean program name with its path will always be available so when can it be null?
Writer says that "if that is not available" but when and how it is possible that program name will not be available?
With the
execclass of calls, you specify the program name and program executable separately so you can set it to NULL then.But that quote is actually from the ISO standard (possibly paraphrased) and that standard covers a awfully large range of execution environments from the smallest micro-controller to the latest z10 Enterprise-class mainframe.
Many of those embedded systems would be in the situation where an executable name makes little sense.
From the latest c1x draft:
This means that, if
argcis zero (and it can be), argv[0] is NULL.But, even when
argcis not 0, you may not get the program name, since the standard also states:So, there is no requirement under the standard that a program name be provided. I’ve seen programs use a wide selection of options for this value:
sleepfor a malicious piece of code).sleep).-kshfor the login shell).progname - a program for something).