C ++为什么main工作没有返回值 [英] C++ why main works with no return value

查看:313
本文介绍了C ++为什么main工作没有返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题出于好奇心;在写一个测试程序的main时,我没有从中返回任何东西( > main )。但我宣布主要 int main()。它编译成功。

This question is out of curiosity; while writing a main for a test program, I returned nothing from main(no return statement in main). But I declared main as int main(). And it compiled successfully.

其中,如果有任何其他函数用 int 返回类型,返回 int ,我会收到一个错误

Where as if there is any other function written with a int return type and actually not returning an int,I would get an error


'函数名称必须返回因此,为什么编译器不会对 main 函数抱怨同样的错误?

'Function name' must return value


$ b <

So why compiler doesn't complain the same for main function?

推荐答案

通常不允许控制流到达非空函数的结尾而不返回任何东西。 main 函数的处理方式与标准中指定的不同。

Normally it is not allowed for the control flow to reach the end of a non-void function without returning something. The main function is handled differently, as specified in the standard.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/ n2960.pdf

§3.6.1 / 5

如果控制到达main结尾而没有遇到返回
语句,效果是执行return 0;

If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

至于理由,我不敢肯定。如果有人知道,请将其添加到我的回答或作为评论。

As for the rationale, I'm not sure, honestly. If someone knows, please add it to my answer or as a comment.

这篇关于C ++为什么main工作没有返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆