DEV Community

Cover image for Mystery Behind the First Command Line Argument in C
Akash Dev
Akash Dev

Posted on • Updated on • Originally published at coolcoderr.hashnode.dev

Mystery Behind the First Command Line Argument in C

I am learning how to write C programs and how to use command line arguments in C.

Command line arguments are the inputs that I give to the program when I run it. The first input is always the name of the program file, followed by the other inputs that I need. The last input is always NULL, which means there are no more inputs.

Why is the program file name always the first input, even if I don’t give any other inputs?

The reason is that the C standard requires that the first argument in the argv array be the name of the program or a string containing the program name, or be a null pointer if the program name is not available. This is to provide a consistent and convenient way of identifying the program that is being executed and to allow the program to access its own name if needed.

For example, some programs use their name to display usage information or error messages or to change their behavior based on the name. The C standard does not specify how the program name is determined, and it may depend on the operating system or the compiler. For example, some systems may use the full path of the executable file, while others may use only the base name. Some systems may allow the user to change the program name by using a symbolic link or an alias.

However, the C standard guarantees that the first argument will always be the program name or a null pointer, and that the last argument will always be a null pointer.

If the first argument was not the file name and there were no other arguments, then the first argument would be NULL, which would be the same as the last argument.

This would make it impossible to distinguish between the two cases and it would cause problems for looping over the argv array or passing it to other functions. For example, if you have a function that prints all the arguments in the argv array, such as:

#include <stdio.h>

void print_args(char **argv);

int main(int argc, char **argv) {
    printf("Number of arguments: %d\n", argc);
    print_args(argv);
    return 0;
}

void print_args(char **argv) {
    while (*argv != NULL) {
        printf("%s\n", *argv);
        argv++;
    }
}
Enter fullscreen mode Exit fullscreen mode

This function will work correctly if the first argument is the file name, but it will fail if the first argument is not the file name and there are no other arguments. In that case, the function will try to dereference a null pointer, which will cause a segmentation fault. Therefore, to avoid this problem, the first argument is always the file name or a null pointer, and the last argument is always a null pointer.

Top comments (5)

Collapse
 
pauljlucas profile image
Paul J. Lucas

How can you mention argv and not mention argc? Or even give the typical signature of main()?

And the program name is never NULL. The only time argv[0] would be NULL is if argc is 0.

The complete — and correct — description of command-line arguments is here, section 5.1.2.2.1, paragraph 2, on page 12.

Collapse
 
akashdev23 profile image
Akash Dev

The code I've mentioned is just a pseudo code to explain how things work not an actual code. argc stores the number of command line arguments but here I was not talking about that that's why I thought it isn't necessary to mention that. I understand that a program name can't be null. It's a mistake thanks for telling me I'll correct it.

Collapse
 
pauljlucas profile image
Paul J. Lucas

No, you actually have to give a code example in pseudocode for it to be pseudocode. The only code (pseudo or otherwise) you give is for print_args().

Regardless, one typically uses pseudocode only to express an algorithm in a language-agnostic way. You're talking specifically about C. Why would you even use pseudocode? Why not use real C?

Thread Thread
 
akashdev23 profile image
Akash Dev

I've corrected that. Thanks for mentioning.

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.