C Command-Line Arguments
When executing a program, values can be passed to a C program from the command line. These values are calledCommand-Line Arguments, and they are important to the program, especially when you want to control the program from the outside rather than hard-coding these values in the code.
In C, command-line arguments are a way to get input from the command line and can be used to pass information to a program when it runs. Command-line arguments are passed to the program through the parameters of the main function. The prototype of the main function can take 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,argcis at 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 program name. The following elements are the command-line arguments passed to the program.
The following is a simple example that checks whether arguments are provided on the command line and performs the corresponding action based on them:
Example
#include
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 code above, and it produces the following result:
$./a.out testing
The argument supplied is testing
Using two arguments, compile and execute the code above, and it produces the following result:
$./a.out testing1 testing2
Too many arguments supplied.
Without passing any arguments, compile and execute the code above, and it produces the following result:
$./a.out
One argument expected
It should be noted thatargv[0] stores the program name,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,argc will be set to 2.
Multiple command-line arguments are separated by spaces, but if an argument itself contains spaces, then when passing it you should place it inside double quotes "" or single quotes ''. Let us rewrite the example above: with a space, you can put it inside double or single quotes """" by this view. Let us rewrite the example above to pass a command-line argument placed inside double quotes to the program:
Example
#include
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 spaces, with the argument enclosed in double quotes, compile and execute the code above, and it produces the following result:
$./a.out "testing1 testing2"
Progranm name ./a.out
The argument supplied is testing1 testing2
Use Cases
Command-line arguments are useful in many situations, for example:
- Configuration file paths
- Mode selection (e.g., debug mode)
- Input and output file names
- Runtime options and flags (such as
-vindicating 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.