为什么main()中不需要return语句? [英] Why does main() not need a return statement?
问题描述
块引用>为什么C中的main()函数可以正确终止即使不使用
退出
或收益
?例如:
#包括LT&;&stdio.h中GT;INT总和(INT A,INT B)
{
返回(A + B);
}诠释的main()
{
INT A = 10;
INT B = 5;
答INT;
ANS =总和(A,B);
的printf(总和%D,ANS);
}
解决方案由于编译器增加了一个隐含的
退出
为您服务。检查为什么nightcracker;我还是强烈建议你做一个有意义的回报code一个明确的退出/返回。
C99标准:
如果main函数的返回值类型是int类型的类型兼容
从到主功能的初始呼叫的返回相当于
调用由主函数的返回值exit函数
作为参数; 10)到达}终止的主要功能
如果返回类型是不兼容INT返回0值,
返回到主机环境的终止状态是
不确定的。
块引用>我要寻找的C89文本进行检查。
Possible Duplicate:
return 0 implicit
Why is return 0 optional?why does the main() function in C can terminate correctly even without using
exit
orreturn
?for example:
#include<stdio.h> int sum(int a,int b) { return (a + b); } int main() { int a=10; int b=5; int ans; ans=sum(a,b); printf("sum is %d",ans); }
解决方案Because the compiler adds an implicit
exit
for you.Check nightcracker on why; still I strongly recommend you make an explicit exit/return with a meaningful return code.
C99 standard:
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;10) 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.
I am looking for the C89 text to check.
这篇关于为什么main()中不需要return语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!