C++ void main 和 int main 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/636829/
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
Difference between void main and int main?
提问by Kredns
Does it matter which way I declare my C++ programs?
我声明我的 C++ 程序的方式有关系吗?
回答by Alan
The difference is one is the correct way to define main
, and the other is not.
区别在于一种是正确的定义方式main
,另一种则不是。
And yes, it does matter. Either
是的,这很重要。任何一个
int main(int argc, char** argv)
or
或者
int main()
are the proper definition of your main
per the C++ spec.
是根据main
C++ 规范对您的正确定义。
void main(int argc, char** argv)
void main(int argc, char** argv)
is not and was, IIRC, a perversity that came with older Microsoft's C++ compilers.
不是,而且是,IIRC,旧的 Microsoft 的 C++ 编译器带来的反常。
回答by vobject
Bjarne Stroustrupmade this quite clear:
Bjarne Stroustrup 说得很清楚:
The definition
void main()
is not and never has been C++, nor has it even been C.
定义
void main()
不是,也从来不是 C++,甚至也不是 C。
See reference.
见参考。
回答by RedBlueThing
You should use int main
. Both the C and C++ standards specify that main
should return a value.
你应该使用int main
. C 和 C++ 标准都指定main
应该返回一个值。
回答by Joe
For C++, only int is allowed. For C, C99 says only int is allowed. The prior standard allowed for a void
return.
对于 C++,只允许使用 int。对于 C,C99 说只允许 int。先前的标准允许void
退货。
In short, always int
.
总之,总是int
。
回答by Svante
The point is, C programs (and C++ the same) always (should?) return a success value or error code, so they should be declared that way.
关键是,C 程序(和 C++ 相同)总是(应该?)返回一个成功值或错误代码,所以它们应该以这种方式声明。
回答by X-Istence
A long time ago I found this page (void main(void))which contained many reasons outside of the "the standard says it is not valid" argument. On particular operating systems/architectures it could cause the stack to become corrupted and or other nasty things to happen.
很久以前,我发现这个页面 (void main(void))包含“标准说它无效”论点之外的许多原因。在特定的操作系统/架构上,它可能会导致堆栈损坏和/或其他令人讨厌的事情发生。
回答by Electrons_Ahoy
If you're going by the spec, then you should always declare main
returning an int
.
如果您按照规范进行操作,那么您应该始终声明main
返回一个int
.
In reality, though, most compilers will let you get away with either one, so the real difference is if you want / need to return a value to the shell.
但实际上,大多数编译器都会让您摆脱其中任何一个,因此真正的区别在于您是否希望/需要向 shell 返回一个值。
回答by Masked Man
In C++, main()
must return int
. However, C99 allows main()
to have a non-int
return type. Here is the excerpt from the C99 standard.
在 C++ 中,main()
必须返回int
. 但是,C99 允许main()
具有非int
返回类型。这是 C99 标准的摘录。
5.1.2.2.1 Program startup
5.1.2.2.1 程序启动
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
程序启动时调用的函数名为 main。实现声明没有此函数的原型。它应定义为返回类型为 int 且不带参数:
int main(void) { /* ... */ }
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
或带有两个参数(此处称为 argc 和 argv,尽管可以使用任何名称,因为它们对于声明它们的函数而言是本地的):
int main(int argc, char *argv[]) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
或同等学历; 或以其他一些实现定义的方式。
Also note that gcc does compile void main()
although practically, it does a return 0;
on encountering a closing brace.
还要注意 gcc 确实可以编译,void main()
尽管实际上它会return 0;
遇到右括号。