为什么main()中不需要return语句? [英] Why does main() not need a return statement?

查看:542
本文介绍了为什么main()中不需要return语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  返回0隐结果
  为什么返回0可选?


为什么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 or return?

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屋!

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