Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any standard for the argument format of a program? [duplicate]

int main(int argc, char **argv)
{}
  1. Is it possible that argc equals 0?

  2. Must argv[0] be the executable file's name?

  3. Is there any standard for these issues?

like image 946
xmllmx Avatar asked Nov 27 '25 01:11

xmllmx


2 Answers

  1. Yes.

  2. Normally yes, it could also be empty, or some other identifying string, or even NULL (see my addendum). It's also possible to change argv[0] to something else from inside the program.

  3. The C (and C++) specifications.

You also missed one: The last element in argv is always NULL, meaning argv[argc] will always be NULL.


In the C11 specification it's in §5.1.2.2.1 Program startup.

In the C++11 specification its §3.6.1 Main function.

like image 54
Some programmer dude Avatar answered Nov 29 '25 14:11

Some programmer dude


  1. Yes
  2. No. Argv[0] don't have to exist, but if it does it is a program name, given it could be obtained.
  3. I guess this is standard in C, Cpp. @JonathanLeffler linked a great answer.
like image 44
MKK Avatar answered Nov 29 '25 14:11

MKK