C++ int argc, char *argv[] 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3024197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does int argc, char *argv[] mean?
提问by Greg Treleaven
In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:
在许多 C++ IDE 和编译器中,当它为您生成 main 函数时,它看起来像这样:
int main(int argc, char *argv[])
When I code C++ without an IDE, just with a command line compiler, I type:
当我在没有 IDE 的情况下编写 C++ 代码时,只使用命令行编译器,我输入:
int main()
without any parameters. What does this mean, and is it vital to my program?
没有任何参数。这是什么意思,它对我的程序至关重要吗?
回答by meagar
argv
and argc
are how command line arguments are passed to main()
in C and C++.
argv
以及argc
如何main()
在 C 和 C++中传递命令行参数。
argc
will be the number of strings pointed to by argv
. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.
argc
将是指向的字符串数argv
。这将(实际上)是 1 加上参数的数量,因为几乎所有的实现都将程序的名称添加到数组中。
The variables are named argc
(argument count) and argv
(argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings)
is equally valid.
变量被命名为argc
( argument count) 和argv
( argument vector),但它们可以被赋予任何有效的标识符:int main(int num_args, char** arg_strings)
同样有效。
They can also be omitted entirely, yielding int main()
, if you do not intend to process command line arguments.
int main()
如果您不打算处理命令行参数,它们也可以完全省略,产生。
Try the following program:
试试下面的程序:
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
Running it with ./test a1 b2 c3
will output
运行它./test a1 b2 c3
会输出
Have 4 arguments:
./test
a1
b2
c3
回答by John Boker
argc
is the number of arguments being passed into your program from the command line and argv
is the array of arguments.
argc
是从命令行传递给程序的参数数量,是参数argv
数组。
You can loop through the arguments knowing the number of them like:
您可以循环遍历参数,知道它们的数量,例如:
for(int i = 0; i < argc; i++)
{
// argv[i] is the argument at index i
}
回答by Toby Speight
Suppose you run your program thus (using sh
syntax):
假设你这样运行你的程序(使用sh
语法):
myprog arg1 arg2 'arg 3'
If you declared your main as int main(int argc, char *argv[])
, then (in most environments), your main()
will be called as if like:
如果您将 main 声明为int main(int argc, char *argv[])
,那么(在大多数环境中),您main()
将像这样调用:
p = { "myprog", "arg1", "arg2", "arg 3", NULL };
exit(main(4, p));
However, if you declared your main as int main()
, it will be called something like
但是,如果您将 main 声明为int main()
,它将被称为
exit(main());
and you don't get the arguments passed.
你没有得到通过的参数。
Two additional things to note:
另外需要注意的两点:
- These are the only two standard-mandated signatures for
main
. If a particular platform accepts extra arguments or a different return type, then that's an extension and should not be relied upon in a portable program. *argv[]
and**argv
are exactly equivalent, so you can writeint main(int argc, char *argv[])
asint main(int argc, char **argv)
.
- 这些是
main
. 如果特定平台接受额外的参数或不同的返回类型,那么这是一个扩展,不应在可移植程序中依赖。 *argv[]
并且**argv
完全等效,因此您可以写int main(int argc, char *argv[])
为int main(int argc, char **argv)
.
回答by BlueMonkMN
The parameters to main
represent the command line parameters provided to the program when it was started. The argc
parameter represents the number of command line arguments, and char *argv[]
is an array of strings (character pointers) representing the individual arguments provided on the command line.
的参数main
表示提供到当它被启动该程序的命令行参数。该argc
参数表示命令行参数的数量,并且char *argv[]
是一个字符串数组(字符指针),表示命令行上提供的各个参数。
回答by moshtagh
The main
function can have two parameters, argc
and argv
. argc
is an integer (int
) parameter, and it is the number of arguments passed to the program.
该main
函数可以有两个参数,argc
和argv
。argc
是一个整数 ( int
) 参数,它是传递给程序的参数数量。
The program name is always the first argument, so there will be at least one argument to a program and the minimum value of argc
will be one. But if a program has itself two arguments the value of argc
will be three.
程序名称始终是第一个参数,因此程序至少有一个参数,并且 的最小值argc
为 1。但是如果程序本身有两个参数,则 的值argc
将是三个。
Parameter argv
points to a string array and is called the argument vector. It is a one dimensional string array of function arguments.
参数argv
指向一个字符串数组,称为参数向量。它是一个函数参数的一维字符串数组。
回答by adrian
int main();
This is a simple declaration. It cannot take any command line arguments.
这是一个简单的声明。它不能接受任何命令行参数。
int main(int argc, char* argv[]);
This declaration is used when your program must take command-line arguments. When run like such:
当您的程序必须采用命令行参数时,将使用此声明。当像这样运行时:
myprogram arg1 arg2 arg3
argc
, or Argument Count, will be set to 4 (four arguments), and argv
, or Argument Vectors, will be populated with string pointers to "myprogram", "arg1", "arg2", and "arg3". The program invocation (myprogram
) is included in the arguments!
argc
或 Argument Count 将设置为 4(四个参数),而argv
或 Argument Vectors 将填充指向“myprogram”、“arg1”、“arg2”和“arg3”的字符串指针。程序调用 ( myprogram
) 包含在参数中!
Alternatively, you could use:
或者,您可以使用:
int main(int argc, char** argv);
This is also valid.
这也是有效的。
There is another parameter you can add:
您可以添加另一个参数:
int main (int argc, char *argv[], char *envp[])
The envp
parameter also contains environment variables. Each entry follows this format:
该envp
参数还包含环境变量。每个条目都遵循以下格式:
VARIABLENAME=VariableValue
like this:
像这样:
SHELL=/bin/bash
The environment variables list is null-terminated.
环境变量列表以空值结尾。
IMPORTANT:DO NOT use any argv
or envp
values directly in calls to system()
! This is a hugesecurity hole as malicious users could set environment variables to command-line commands and (potentially) cause massive damage. In general, just don't use system()
. There is almost always a better solution implemented through C libraries.
重要提示:不要在调用中直接使用任何argv
或envp
值system()
!这是一个巨大的安全漏洞,因为恶意用户可以将环境变量设置为命令行命令并(可能)造成巨大破坏。一般来说,不要使用system()
. 通过 C 库几乎总是有更好的解决方案。
回答by Nick Gerakines
The first parameter is the number of arguments provided and the second parameter is a list of strings representing those arguments.
第一个参数是提供的参数数量,第二个参数是表示这些参数的字符串列表。
回答by Chris Becke
Both of
两者的
int main(int argc, char *argv[]);
int main();
are legal definitions of the entry point for a C or C++ program. Stroustrup: C++ Style and Technique FAQdetails some of the variations that are possible or legal for your main function.
是 C 或 C++ 程序入口点的合法定义。Stroustrup: C++ Style and Technique FAQ详细介绍了主要函数可能或合法的一些变化。