When passing argument to main() in a C or C++ application, will argv[0] always be the name of the executable? Or is this just a common convention and not guaranteed to be true 100% of the time?
When passing argument to main() in a C or C++ application, will argv[0] always
Share
Guesswork (even educated guesswork) is fun but you really need to go to the standards documents to be sure. For example, ISO C11 states (my emphasis):
So no, it’s only the program name if that name is available. And it “represents” the program name, not necessarily is the program name. The section before that states:
This is unchanged from C99, the previous standard, and means that even the values are not dictated by the standard – it’s up to the implementation entirely.
This means that the program name can be empty if the host environment doesn’t provide it, and anything else if the host environment does provide it, provided that “anything else” somehow represents the program name. In my more sadistic moments, I would consider translating it into Swahili, running it through a substitution cipher then storing it in reverse byte order :-).
However, implementation-defined does have a specific meaning in the ISO standards – the implementation must document how it works. So even UNIX, which can put anything it likes into
argv[0]with theexecfamily of calls, has to (and does) document it.