C Command Line Arguments
# C Command Line Arguments
When executing a program, you can pass values to a C program from the command line. These values are called **command line arguments**, and they are very important for programs, especially when you want to control the program from outside rather than hardcoding these values inside the code.
In C, command line arguments are a method of obtaining input from the command line, which can be used to pass information to a program when running it. 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)**: Represents the number of command line arguments, including the program name itself. Therefore, `argc` is 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`) is usually the program name. The subsequent elements are the command line arguments passed to the program.
Here is a simple example that checks whether command line arguments are provided and performs corresponding actions based on the arguments:
## Example
#include
int main(int argc,char*argv[])
{
if( argc ==2)
{
printf("The argument supplied is %sn", argv);
}
else if( argc >2)
{
printf("Too many arguments supplied.n");
}
else
{
printf("One argument expected.n");
}
}
Compile and execute the above code with one argument, and it will produce the following result:
$./a.out testing The argument supplied is testing
Compile and execute the above code with two arguments, and it will produce the following result:
$./a.out testing1 testing2 Too many arguments supplied.
Compile and execute the above code without any arguments, and it will produce the following result:
$./a.outOne argument expected
It should be noted that **argv** stores the program name, **argv** is a pointer to the first command line argument, and *argv 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, you should place the argument inside double quotes "" or single quotes '' when passing it. Let's rewrite the above example to pass a command line argument enclosed in double quotes to the program:
## Example
#include
int main(int argc,char*argv[])
{
printf("Program name %sn", argv);
if( argc ==2)
{
printf("The argument supplied is %sn", argv);
}
else if( argc >2)
{
printf("Too many arguments supplied.n");
}
else
{
printf("One argument expected.n");
}
}
Compile and execute the above code with a simple argument enclosed in double quotes and separated by spaces, and it will produce the following result:
$./a.out "testing1 testing2"Progranm name ./a.outThe argument supplied is testing1 testing2
### Use Cases
Command line arguments are useful in many scenarios, such as:
* Configuration file paths
* Mode selection (e.g., debug mode)
* Input file and output file names
* Runtime options and flags (e.g., `-v` for verbose mode)
### Notes
* Command line arguments are typically strings. If you need to convert them to numeric types, you can use standard library functions such as `atoi` or `strtol`.
* You should always validate and handle command line arguments to prevent input errors or malicious input.
[](#)(#)
(#)[](#)
## 2 Notes Write a Note
1. #0 Blithe
cn1***0441251@126.com [](#)257 The two parameters of main have the following parameter names:
int main( int argc, char *argv[] )
They don't have to be written this way; it's just a convention. However, you can also write them like this:
int main( int test_argc, char *test_argv[] )
Use any names you like.
But most people still write them as shown at the beginning, like this:
int main( int argc, char *argv[] ) (javascript:;)Blithe
cn1***0441251@126.com 8 years ago (2019-01-24)
2. #0 JustSong
son***nderful@qq.com
(http://man7.org/linux/man-pages/man3/getopt.3.html) [](#)170 On Linux, we can use getopt and getopt_long to parse command line arguments, for example:
int main(int argc, char *argv[]){ char *optstr = "p:n:m:c:"; struct option opts[] = { {"path", 1, NULL, 'p'}, {"name", 1, NULL, 'n'}, {"mtime", 1, NULL, 'm'}, {"ctime", 1, NULL, 'c'}, {0, 0, 0, 0}, }; int opt; while((opt = getopt_long(argc, argv, optstr, opts, NULL)) != -1){ switch(opt) { case 'p': strcpy(path, optarg); break; case 'n': strcpy(targetname, optarg); break; case 'm': modifiedtime = atoi(optarg); break; case 'c': changetime = atoi(optarg); break; case '?': if(strchr(optstr, optopt) == NULL){ fprintf(stderr, "unknown option '-%c'n", optopt); }else{ fprintf(stderr, "option requires an argument '-%c'n", optopt); } return 1; } } findInDir(path); return 0;}(javascript:;)JustSong
son***nderful@qq.com
(http://man7.o
YouTip