简体   繁体   English

定义“int main (void)”和“int main (int argc, char *argv[])”有什么区别,什么时候使用?

[英]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:我想了解的是:

  1. When to use int main (void) v/s int main (int argc, char *argv[]) .何时使用int main (void) v/s int main (int argc, char *argv[])
  2. Hypothetically, when the program doesn't expect command-line arguments, is there any difference to the compiler (let's say gcc and clang ) at all?假设,当程序不期望命令行 arguments 时,编译器是否有任何区别(比如说gccclang )?
  3. (Just for the sake of writing self-documenting code) should I use int main (void) when the program doesn't expect command line arguments and int main (int argc, char *argv[]) when it does? (只是为了编写自记录代码)当程序不需要命令行 arguments 和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:阅读本文,我对您的问题的回答是:

  1. Use int main (int argc, char *argv[]) whenever you need to access the program's arguments;每当您需要访问程序的 arguments 时,请使用int main (int argc, char *argv[])
  2. I do not expect to be any difference to the compiler in both ways, once, in this experiment , both C programs had the same assembly output;我不希望这两种方式对编译器有任何区别,一次,在这个实验中,两个 C 程序都有相同的程序集 output;
  3. I recommend you to use int main (void) whenever your program does not need to access the arguments.当您的程序不需要访问 arguments 时,我建议您使用int main (void) This way, it is clear for other people that your program do not use them.这样,其他人就很清楚您的程序不使用它们。 In future, if you need to access the program's arguments, changing the syntax of the main() function is not that hard.将来,如果您需要访问程序的 arguments,更改main() function 的语法并不难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C,“ void main(int argc,char * argv [0])”的含义是,请注意argv中的零 - the C, what does “void main (int argc, char *argv[0])”, note the zero in argv 使用main(int argc,char * argv [])的问题 - Problems using main(int argc,char *argv[]) int main(int argc, const char * argv[]) AND 文件输入 - int main(int argc, const char * argv[]) AND file input 除了“int main(int argc, char **argv)”之外,我可以安全地使用名称中包含“main”的函数吗? - Can I safely use functions containing "main" in their name besides "int main(int argc, char **argv)"? 函数main中的参数argv(int argc,char * argv []) - The parameter argv in the function main(int argc, char *argv[]) int main() 和 int main(void) 之间的区别? - Difference between int main() and int main(void)? 是否必须编写int main(int argc,char * argv []),不能使用其他名称而不是argc和* argv []? - Is it compulsory to write int main(int argc, char *argv[]), can't use some other names instead of argc and *argv[]? 如何在主函数中实现int argc和char * argv []? - How do I implement int argc and char *argv[] in the main function? 使用main(int argc,char ** argv){}作为函数重写程序 - Rewrite a program with main(int argc,char **argv) {} as a function 在 C 中以编程方式调用 main(int argc, char **argv) - Programmatically calling main(int argc, char **argv) in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM