更新时间:2023-08-25 23:04:10
命令行参数是使用名称传递给程序的参数.例如,UNIX程序cp
(复制两个文件)具有以下命令行参数:
Command line arguments are arguments passed to your program with its name. For example, the UNIX program cp
(copies two files) has the following command line arguments:
cp SOURCE DEST
您可以使用argc
和argv
访问命令行参数:
You can access the command line arguments with argc
and argv
:
int main(int argc, char *argv[])
{
return 0;
}
argc是参数的数量,包括程序名,而argv是包含参数的字符串数组. argv[0]
是程序名称,并且保证argv[argc]
是NULL
指针.
argc is the number of arguments, including the program name, and argv is the array of strings containing the arguments. argv[0]
is the program name, and argv[argc]
is guaranteed to be a NULL
pointer.
因此cp
程序可以这样实现:
So the cp
program can be implemented as such:
int main(int argc, char *argv[])
{
char *src = argv[1];
char *dest = argv[2];
cpy(dest, src);
}
不必将它们命名为argc
和argv
;它们可以有您想要的任何名称,尽管传统上它们被称为该名称.
They do not have to be named argc
and argv
; they can have any name you want, though traditionally they are called that.