C++ int main(int argc, char** argv)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5191965/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:37:58  来源:igfitidea点击:

int main(int argc, char** argv)

c++argumentsmain

提问by Simplicity

Duplicate:
What is the proper declaration of main?

重复:
main 的正确声明是什么?

What do we mean by the arguments in this mainfunction? What are they trying to tell us?

这个main函数中的参数是什么意思?他们想告诉我们什么?

int main(int argc, char** argv)

int main(int argc, char** argv)

UPDATE:And, is the preceding line of code similar to this int main(int argc, char* argv[])? If so, how can we say that char** argvis similar to char* argv[]as they don't look similar from an arraypoint of view?

更新:而且,前面的代码行是否与此类似int main(int argc, char* argv[])?如果是这样,我们怎么能说char** argv类似char* argv[],因为他们不看从类似阵列的观点?

How is it compared with int main()which does not have any arguments?

它与int main()没有任何参数的相比如何?

Thanks.

谢谢。

回答by trojanfoe

The argcparameter is the number of command line options specified, including the executable name, when the executable was invoked. The individual command line options are found in the argvarray, which is NULLterminated (the name and path used to invoke the executable is in argv[0]).

argc参数是在调用可执行文件时指定的命令行选项数,包括可执行文件名称。单个命令行选项argv位于NULL终止的数组中(用于调用可执行文件的名称和路径在 中argv[0])。

The difference between the two versions is simply if you want to parse command line arguments or not - if you are not interested in them then you can ignore them using the second form.

两个版本之间的区别很简单,如果你想解析命令行参数——如果你对它们不感兴趣,那么你可以使用第二种形式忽略它们。

回答by Bj?rn Pollex

Wikipediaoffers a good explanation. The first parameter gives you the number of command line arguments, and the second gives you the actual arguments.

维基百科提供了很好的解释。第一个参数为您提供命令行参数的数量,第二个参数为您提供实际参数。

回答by CashCow

They represent the command line parameters.

它们代表命令行参数。

argc is the number of command line parameters, including the name of the executable. argv is an array of null-terminated strings, where argv[0]is the command line parameter, and argv[i]is the ith parameter after that, argv[argc-1]being the last one and argv[argc]is actually well defined and a NULL pointer.

argc 是命令行参数的数量,包括可执行文件的名称。argv 是一个以空字符结尾的字符串数组,其中argv[0]是命令行参数,argv[i]是之后的第 i 个参数,argv[argc-1]是最后一个,argv[argc]实际上定义良好,并且是一个 NULL 指针。

Thus:

因此:

foo bar baz

foo bar baz

on the command line will have argc=3, argv[0]="foo" argv[1]="bar" argv[2]="baz" argv[3]= NULL

在命令行上会有argc=3, argv[0]="foo" argv[1]="bar" argv[2]="baz" argv[3]= NULL

Note that there is no special attachment placed for "flag" arguments.

请注意,没有为“标志”参数放置特殊附件。

grep -i foo bar.cpp bar.h

grep -i foo bar.cpp bar.h

would have 4 arguments (argc=5 including grep itself), -i being one of them and this would apply even if the next parameter was a "value" attached to the flag.

将有 4 个参数(argc=5 包括 grep 本身),-i 是其中之一,即使下一个参数是附加到标志的“值”,这也适用。

Note if you did a wildcard

请注意您是否使用了通配符

grep -i foo *

grep -i foo *

in UNIX at least, the * would be expanded before the call into grep and thus each file that matched would be an argument.

至少在 UNIX 中,* 将在调用之前扩展为 grep,因此每个匹配的文件都将是一个参数。

Incidentally

顺便

char** argvand char* argv[]

char** argvchar* argv[]

do the same thing.

做同样的事。

Also while the standard says you must use one of these signatures (you shouldn't even add in any consts) there is no law you have to use those two variable names, but it is so conventional now that they are pretty much universal. (i.e. you could use argCountand argValuesif you want).

此外,虽然标准规定您必须使用这些签名之一(您甚至不应该添加任何常量),但没有法律规定您必须使用这两个变量名称,但现在它们是如此传统,以至于它们几乎是通用的。(即你可以使用argCountargValues如果你愿意的话)。

回答by Bernd Elkemann

argc gives you the number of arguments and argv gives you those arguments. The first one is the path to the .exe used to run your program, the following ones are arguments the caller of your .exe provided on the command line like this:

argc 为您提供参数的数量,而 argv 为您提供这些参数。第一个是用于运行程序的 .exe 的路径,以下是 .exe 的调用者在命令行上提供的参数,如下所示:

my.exe arg1 arg2

Whereas

然而

int main() {}

just ignores the arguments.

只是忽略参数。

回答by Kai

argv is an array holding the commandline parameters passed to the application. argc tells you the number of elements contained in that array.

argv 是一个保存传递给应用程序的命令行参数的数组。argc 告诉您该数组中包含的元素数。