C/C++ 中 void main 和 int main 的区别?
在 C++(或 C)程序中声明 main
函数的方式重要吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 C++(或 C)程序中声明 main
函数的方式重要吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
区别在于,一个是定义
main
的正确方法,另一个则不是。是的,这确实很重要。 或
是
根据 C++ 规范对
main
的正确定义。void main(int argc, char** argv)
过去和现在都不是,IIRC,是旧版 Microsoft C++ 编译器附带的反常现象。
https://isocpp.org/wiki/faq/newbie#main-returns-int
The difference is one is the correct way to define
main
, and the other is not.And yes, it does matter. Either
or
are the proper definition of your
main
per the C++ spec.void main(int argc, char** argv)
is not and was, IIRC, a perversity that came with older Microsoft's C++ compilers.
https://isocpp.org/wiki/faq/newbie#main-returns-int
Bjarne Stroustrup 说得很清楚:
参见 参考。
Bjarne Stroustrup made this quite clear:
See reference.
您应该使用
int main
。 C 和 C++ 标准都指定main
应返回一个值。You should use
int main
. Both the C and C++ standards specify thatmain
should return a value.对于 C++,仅允许使用 int。 对于 C,C99 规定只允许使用 int。 先前的标准允许
void
返回。简而言之,始终是
int
。For C++, only int is allowed. For C, C99 says only int is allowed. The prior standard allowed for a
void
return.In short, always
int
.关键是,C 程序(和 C++ 相同)总是(应该?)返回成功值或错误代码,因此应该以这种方式声明它们。
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.
很久以前,我发现 此页面 (void main(void)) 其中包含除了“标准说它无效”的论点之外,还有许多原因。 在特定的操作系统/体系结构上,它可能会导致堆栈损坏和/或发生其他令人讨厌的事情。
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.
在 C++ 中,
main()
必须返回int
。 但是,C99 允许main()
有非int
返回类型。 以下是 C99 标准的摘录。5.1.2.2.1 程序启动
程序启动时调用的函数名为 main。 实施声明没有
该函数的原型。 它应该被定义为返回类型 int 并且没有
参数:
int main(void) { /* ... */ }
或带有两个参数(此处称为 argc 和 argv,但可以是任何名称)
使用,因为它们是声明它们的函数的本地函数):
int main(int argc, char *argv[]) { /* ... */ }
或同等水平; 或以其他一些实现定义的方式。
另请注意,gcc 确实会编译
void main()
,但实际上,它会在遇到右大括号时执行return 0;
。In C++,
main()
must returnint
. However, C99 allowsmain()
to have a non-int
return type. Here is the excerpt from the C99 standard.5.1.2.2.1 Program startup
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:
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):
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 areturn 0;
on encountering a closing brace.如果您遵循规范,那么您应该始终声明
main
返回int
。但实际上,大多数编译器都会让您选择其中任何一种,因此真正的区别在于您是否想要/需要向 shell 返回一个值。
If you're going by the spec, then you should always declare
main
returning anint
.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.