[英]What is the difference between the definitions “int main (void)” and “int main (int argc, char *argv[])” and when to use which?
As per the current standard and cppreference these two are the correct way to define the main()
function.根据当前标准和cppreference ,这两个是定义
main()
function 的正确方法。 I know what the parameters mean in either definition, and am not seeking clarifications on that aspect.我知道这两个定义中的参数是什么意思,我并不寻求对此方面的澄清。 What I'd like to understand is:
我想了解的是:
int main (void)
v/s int main (int argc, char *argv[])
.int main (void)
v/s int main (int argc, char *argv[])
。gcc
and clang
) at all?gcc
和clang
)?int main (void)
when the program doesn't expect command line arguments and int main (int argc, char *argv[])
when it does? int main (int argc, char *argv[])
时,我应该使用int main (void)
吗? I think you can get a good answer here .我想你可以在这里得到一个很好的答案。 Reading this article, my answers to your questions are:
阅读本文,我对您的问题的回答是:
int main (int argc, char *argv[])
whenever you need to access the program's arguments;int main (int argc, char *argv[])
;int main (void)
whenever your program does not need to access the arguments.int main (void)
。 This way, it is clear for other people that your program do not use them.main()
function is not that hard.main()
function 的语法并不难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.