Cprogramming 简明教程
Command Line Arguments in C
在任何 C 程序中都可以有多个函数,但 main() 函数仍然是执行开始时的入口点。虽然其他函数可能有一个或多个参数和一个返回类型,但 main() 函数通常不带参数编写。main() 函数的返回值也为“0”。
In any C program, there may be multiple functions, but the main() function remains the entry point from where the execution starts. While the other functions may have one or more arguments and a return type, the main() function is generally written with no arguments. The main() function also has a return value of "0".
int main() {
...
...
return 0;
}
在 main() 函数中可能存在 scanf() 语句,以允许用户输入某些值,然后使用这些值。
Inside the main() function, there may be scanf() statements to let the user input certain values, which are then utilized.
#include <stdio.h>
int main() {
int a;
scanf("%d", &a);
printf("%d", a);
return 0;
}
What are Command Line Arguments?
除了从程序内部调用输入语句之外,还可以在执行程序时从命令行向 main() 函数传递数据。这些值称为 command line arguments 。
Instead of invoking the input statement from inside the program, it is possible to pass data from the command line to the main() function when the program is executed. These values are called command line arguments.
命令行参数对您的程序非常重要,尤其是当您希望从外部控制程序,而不是在代码中对这些值进行硬编码时。
Command line arguments are important for your program, especially when you want to control your program from outside, instead of hard coding those values inside the code.
假设您想编写一个 C 程序“hello.c”,为用户打印一条“hello”消息。除了使用 scanf() 从程序内部读取名称之外,我们希望从命令行传递名称,如下所示 −
Let us suppose you want to write a C program "hello.c" that prints a "hello" message for a user. Instead of reading the name from inside the program with scanf(), we wish to pass the name from the command line as follows −
C:\users\user>hello Prakash
字符串将被用作 main() 函数的参数,然后将显示“Hello Prakash”消息。
The string will be used as an argument to the main() function and then the "Hello Prakash" message should be displayed.
argc and argv
若要简化 main() 函数接受命令行参数,您应该在 main() 函数中定义两个参数 - argc 和 argv[] 。
To facilitate the main() function to accept arguments from the command line, you should define two arguments in the main() function – argc and argv[].
argc 指的是已传递的参数数量,而 argv[] 是一个指向已传递给程序的每个参数的指针数组。
argc refers to the number of arguments passed and argv[] is a pointer array that points to each argument passed to the program.
int main(int argc, char *argv[]) {
...
...
return 0;
}
argc 参数应始终为非负数。 argv 参数是所有参数的字符指针数组,其中 argv[0] 是程序的名称。在此之后,直到“ argv [argc - 1] ”,每个元素都是一个命令行参数。
The argc argument should always be non-negative. The argv argument is an array of character pointers to all the arguments, argv[0] being the name of the program. After that till "argv [argc - 1]", every element is a command-line argument.
打开任意文本编辑器并将以下代码另存为“ hello.c ” −
Open any text editor and save the following code as "hello.c" −
#include <stdio.h>
int main (int argc, char * argv[]){
printf("Hello %s", argv[1]);
return 0;
}
该程序将从 argv[1] 中获取名称,并在 printf() 语句中使用它。
The program is expected to fetch the name from argv[1] and use it in the printf() statement.
不要通过任何 IDE 的“运行”菜单(例如 VS Code 或 CodeBlocks)运行程序,请从命令行编译它−
Instead of running the program from the Run menu of any IDE (such as VS code or CodeBlocks), compile it from the command line −
C:\Users\user>gcc -c hello.c -o hello.o
生成可执行文件−
Build the executable −
C:\Users\user>gcc -o hello.exe hello.o
将名称作为命令行参数传递−
Pass the name as a command line argument −
C:\Users\user>hello Prakash
Hello Prakash
如果在 Linux 上运行,则默认情况下编译会生成对象文件“ a.out ”。在运行之前我们需要生成它作为可执行文件,将“./”作为前缀。
If working on Linux, the compilation by default generates the object file as "a.out". We need to make it executable before running it by prefixing "./" to it.
$ chmod a+x a.o
$ ./a.o Prakash
下面是一个简单示例,检查是否从命令行提供了任何参数,并相应采取措施−
Given below is a simple example that checks if there is any argument supplied from the command line and takes action accordingly −
#include <stdio.h>
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");
}
}
当上述代码编译并使用单个参数执行时,将生成以下内容输出−
When the above code is compiled and executed with a single argument, it produces the following output −
$./a.out testing
The argument supplied is testing.
当上述代码编译并使用两个参数执行时,将生成以下内容输出−
When the above code is compiled and executed with two arguments, it produces the following output −
$./a.out testing1 testing2
Too many arguments supplied.
当上述代码编译并执行时,没有传递任何参数,将生成以下内容输出−
When the above code is compiled and executed without passing any argument, it produces the following output −
$./a.out
One argument expected
值得注意的是, argv[0] 保存程序本身的名称, argv[1] 是指向所提供的第一个命令行参数的指针, *argv[n] 是最后一个参数。如果没有提供参数,则 argc 将设置为“ 1 ”,如果您传递一个参数,则 argc 将设置为“ 2 ”。
It should be noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, then argc will be set at "1" and if you pass one argument, then argc is set at "2".
Passing Numeric Arguments from the Command Line
我们编写一个 C 程序,该程序读取两个命令行参数,并执行 argv[1] 和 argv[2] 的加法。
Let us write a C program that reads two command line arguments, and performs the addition of argv[1] and argv[2].
Example
从保存以下代码开始−
Start by saving the code below −
#include <stdio.h>
int main (int argc, char * argv[]) {
int c = argv[1] + argv[2];
printf("addition: %d", c);
return 0;
}
当我们尝试编译时,您会收到错误消息−
When we try to compile, you get the error message −
error: invalid operands to binary + (have 'char *' and 'char *')
int c = argv[1]+argv[2];
~~~~~~~~~~~~~~
这是因为“+”运算符不能有非数字操作数。
This is because the "+" operator cannot have non-numeric operands.
The atoi() Function
要解决此问题,我们需要使用库函数 atoi() ,该函数将数字的字符串表示形式转换为整数。
To solve this issue, we need to use the library function atoi() that converts the string representation of a number to an integer.
以下示例显示了如何在 C 程序中使用 atoi() 函数−
The following example shows how you can use the atoi() function in a C program −
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char * argv[]) {
int c = atoi(argv[1]) + atoi(argv[2]);
printf("addition: %d", c);
return 0;
}
从“add.c”编译并构建一个可执行文件,并通过从命令行传递数值参数运行它−
Compile and build an executive from "add.c" and run from the command line, passing numeric arguments −
C:\Users\user>add 10 20
addition: 30
您将所有命令行参数用空格分开,但如果参数本身带有空格,则您可以通过将它们放在双引号 (“ “) 或单引号 (' ') 中传递这些参数。
You pass all the command line arguments separated by a space, but if the argument itself has a space, then you can pass such arguments by putting them inside double quotes (" ") or single quotes (' ').
在此示例中,我们将传递一个用双引号引起来命令行参数−
In this example, we will pass a command line argument enclosed inside double quotes −
#include <stdio.h>
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");
}
}
编译以上代码并执行一个以空格分隔但位于双引号内的参数时,会生成以下输出−
When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output −
$./a.out "testing1 testing2"
Program name ./a.out
The argument supplied is testing1 testing2