C Command-line arguments
When a program is executed, values can be passed to a C program from the command line. These values are calledCommand-line argumentsThey are important to the program, especially when you want to control the program from outside rather than hard-coding these values in the code.
In C language, command line arguments are a method of obtaining input from the command line, which can be used to pass information to the program when running the program. Command line arguments are passed to the program through the parameters of the main function. The prototype of the main function can be one of the following two forms:
int main(int argc, char *argv[]);
or:
int main(int argc, char **argv);
argc(argument count): indicates the number of command-line arguments, including the program name itself. Therefore,argcAt least 1.argv(argument vector): is a pointer to an array of strings, where each string is a command-line argument. The first element of the array (i.e.,argv[0]) is usually the name of the program. The following elements are the command-line arguments passed to the program.
The following is a simple example that checks whether command line arguments are provided and performs corresponding actions based on the arguments:
Example
int main( int argc, char *argv[] )
{
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
}
Using one argument, compile and execute the above code, it will produce the following result:
$./a.out testing The argument supplied is testing
Using two arguments, compile and execute the above code, it will produce the following result:
$./a.out testing1 testing2 Too many arguments supplied.
Passing no arguments, compile and execute the above code, it will produce the following result:
$./a.out One argument expected
It should be noted that,argv[0]stores the name of the program,argv[1]is a pointer to the first command-line argument, and *argv[n] is the last argument. If no arguments are provided, argc will be 1; otherwise, if one argument is passed,argcwill be set to 2.
Multiple command line arguments are separated by spaces, but if the argument itself contains spaces, then when passing arguments you should place the argument inside double quotes "" or single quotes ''. Let's rewrite the above example, there is a space, then you can, from this point of view, put them in double quotes or single quotes "". Let's rewrite the above example to pass a command line argument placed inside double quotes to the program:
Example
int main( int argc, char *argv[] )
{
printf("Program name %s\n", argv[0]);
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
}
Using a simple argument separated by a space, with the argument enclosed in double quotes, compile and execute the above code, it will produce the following result:
$./a.out "testing1 testing2" Progranm name ./a.out The argument supplied is testing1 testing2
Use Case
Command-line arguments are useful in many situations, for example:
- Configuration file path
- Mode selection (e.g., debug mode)
- Input file and output file names
- Runtime options and flags (such as
-vindicates verbose mode)
Notes
- Command-line arguments are usually strings; if you need to convert them to numeric types, you can use standard library functions such as
atoiorstrtol。 - You should always validate and handle command-line arguments to prevent incorrect or malicious input.