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++;
}
}
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)
How can you mention
argv
and not mentionargc
? Or even give the typical signature ofmain()
?And the program name is never
NULL
. The only timeargv[0]
would beNULL
is ifargc
is 0.The complete — and correct — description of command-line arguments is here, section 5.1.2.2.1, paragraph 2, on page 12.
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.
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?
I've corrected that. Thanks for mentioning.
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:
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.