DEV Community

Discussion on: Mystery Behind the First Command Line Argument in C

Collapse
 
ennor profile image
Enno Rehling (恩諾)

You mention the C standard without citing it, and at least in one case, you're mistaken: It's not required that argv[0] is the name of the program, for example. Sometimes it's actually an empty string. ISO C11 says:

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.

The Windows API for example has a function to let you get the program name: GetModuleFileName

I don't know if NULL-termination of argv is guaranteed, either - you should probably just use argc to find the end of the array.