为什么,如果 c++标准说语法不正确,g++ 确实如此吗?允许吗?
我刚刚读到一条评论,内容类似于:
“你永远不应该使用 void main()
,你应该始终使用 int main()
。”
现在我知道使用 int main()
的原因(以便您可以检查返回是否成功等等),但我不知道使用 void main()
是非法的。我做了一些调查,我发现不使用 void main() 的唯一原因是因为“标准是这样说的”。
我的问题是: 如果 C++ 标准规定 main 必须返回一个值,为什么 g++ 允许程序员使用 void main()
作为有效语法?它不应该返回错误/警告吗,因为它违反了标准的规定?
I just read a comment that said something along the likes of:
"You should never use void main()
you should always use int main()
."
Now I know the reasons for using int main()
(so that you can check for success on return and whatnot) but I didn't know that using void main()
was illegal. I did some investigating and the only reason I could find not use void main()
is because the "standard says so".
My question is:
Why, if the C++ standard says that main must return a value, does g++ allow programmers to use void main()
as valid syntax? Shouldn't it return an error / warning because it goes against what the standard says?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这仅意味着您的编译器的特定版本可能允许它,但更高版本(可能更符合标准)可能不允许它。因此,最好从一开始就编写符合标准的代码!
That only means that a particular version of your compiler may allow it, but the later versions (which is likely to be more Standard conformant) may not allow it. So better write Standard Conformant code from the beginning!
按照标准,main确实需要返回int。但许多编译器允许 void 返回类型,因为在标准之前的 C++ 中它是允许的,并且很长一段时间以来,许多代码都是使用 void 返回类型编写的。
还值得一提的是,C++ 明确允许省略 void:
will return 0 的 return 语句。但这仅适用于 main。
According to the standard, main is indeed required to return int. But many compilers allow a return type of void since in pre-standard C++ it was allowed, and for a long time much code was written with a return type of void.
It is also worth to mention that C++ explicitly allows obission of the return statement for void:
will return 0. But that is only allowed for main.
您可以使用以下构建命令强制编译器符合标准:
如果您不编写跨平台代码,那么
-c99
可能是更好的选择。并非所有编译器都支持这一点。You can force the compiler to be standards compliment by using the following the build commands:
If you are not coding cross-platform code then
-c99
might be a better choice. Not all compilers support that.GNU 项目有一个对其哲学的体面总结:
有时,当扩展引起像这样的混乱时,GCC 会删除它们。我相信这个扩展的存在是为了允许具有不正确
main
声明的旧代码进行编译,而不一定是鼓励人们编写void main()
。类似于允许 POSIX 之前的函数声明的扩展。此外,虽然int main(int argc, const char** argv)
是 C 认可的main
声明,但 C++ 标准也认可int main()
,以及 POSIX 制裁int main(int argc, const char** argv, const char** envp)
。很可能还有其他我还没有遇到的声明。The GNU Project has a decent summary of their philosophy:
Sometimes, GCC has removed extensions when they caused confusion like this one. I believe this extension existed to allow old code with an incorrect
main
declaration to compile, not necessarily to encourage people writingvoid main()
. Similar to the extension that allowed pre-POSIX function declarations. Besides, whileint main(int argc, const char** argv)
is the C-approved declaration formain
, the C++ standard also sanctionsint main()
, and POSIX sanctionsint main(int argc, const char** argv, const char** envp)
. There may well be other declarations that I haven't run into yet.