程序编译时返回 0 或不返回 0

发布于 2024-09-12 11:16:02 字数 402 浏览 1 评论 0原文

可能的重复:
C/C++ 中 main() 应该返回什么?

这是一个非常基本的问题问题,我猜。

我已经编程一年了,但我的一个朋友提出了一个相当令人困惑的问题,让我感到惊讶。

在 C++ 上以“int main()”开头的程序似乎可以完美编译,即使使用“return 0;”已删除且不被任何其他返回语句替换。并且根本没有 return 语句,程序仍然显示“Process returned 0”。

这有什么解释吗? 抱歉,如果我的问题很愚蠢!

Possible Duplicate:
What should main() return in C/C++?

This is a pretty basic question, I guess.

I've been programming for a year now but a friend of mine surprised me with a rather stupefying question.

Programs that start with 'int main()' on C++ seem to compile perfectly even with 'return 0;' removed and not replaced by any other return statement. And without a return statement at all, the program still shows that 'Process returned 0'.

Does this have any explanation?
Sorry if my question is silly!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空袭的梦i 2024-09-19 11:16:02

§3.6.1/5:

main 中的 return 语句具有离开主函数(销毁具有自动存储持续时间的任何对象)并以返回值作为参数调用 exit 的效果。如果控制到达 main 末尾而没有遇到 return 语句,则效果是执行
返回0;

§3.6.1/5:

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing
return 0;

时光匆匆的小流年 2024-09-19 11:16:02

来自 What should main() return in C/C++? 的接受答案

还值得注意的是,在 C++ 中,int main() 可以不带返回值,此时它默认返回 0。对于 C99 程序也是如此。是否应该省略 return 0 有争议。

From the accepted answer of What should main() return in C/C++?

It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0. This is also true with a C99 program. Whether return 0 should be omitted or not is open to debate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文