为什么主函数没有返回值就可以工作? [英] Why does the main function work with no return value?

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

问题描述

这个问题出于好奇;在为测试程序编写主程序时,我没有从 main 返回任何内容(在 return 语句>主要)。但我将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


函数名必须返回值

'Function name' must return value

那么为什么编译器对 main 函数没有相同的抱怨?

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

推荐答案

通常不允许控制流在不返回任何内容的情况下到达非void函数的末尾。 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
语句,则效果是执行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.

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

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