Can I omit return from main in C? [duplicate]

我的梦境 提交于 2019-12-28 06:45:17

问题


In C++, 3.6.1 Main function

(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;

Can I do the following in C99 without return 0?

int main() { }

回答1:


Yes, as of C99, reaching the } at the end of main returns 0 if the return type of main is compatible with int.

5.1.2.2.3 Program termination

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;11) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.




回答2:


Yes, the C99 standard says (§5.1.2.2.3):

reaching the } that terminates the main function returns a value of 0.



来源:https://stackoverflow.com/questions/13545291/can-i-omit-return-from-main-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!