为什么主要在这里不返回0? [英] Why main does not return 0 here?

查看:109
本文介绍了为什么主要在这里不返回0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是读

ISO / IEC 9899:201X委员会草案 - 2011年4月12日

在我发现在5.1.2.2.3计划终止

  ..到达}终止主函数返回一个0值。

如果你不指定任何return语句的main(),如果该程序成功运行,然后在右括号}主要将返回0这意味着

但在随后的code我不指定任何return语句,但它不会返回0

 #包括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);
}

编译

  GCC test.c的
./a.out
总和为15
回声$?
9 //这里应该是0,但它显示9,为什么?


解决方案

在1999年版的C标准加入这条规则。在C90,返回的状态是不确定的。

您可以通过传递 -std = C99 来GCC启用它。

作为一个方面说明,有趣9返回,因为它是的printf的返回刚刚写了9个字符。

I was just reading

ISO/IEC 9899:201x Committee Draft — April 12, 2011

in which i found under 5.1.2.2.3 Program termination

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

it means if you don't specify any return statement in main(), and if the program runs successfully, then at the closing brace } of main will return 0.

But in the following code i don't specify any return statement, yet it does not return 0

#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);
}

compile

gcc test.c  
./a.out
sum is 15
echo $?
9          // here it should be 0 but it shows 9 why?

解决方案

That rule was added in the 1999 version of the C standard. In C90, the status returned is undefined.

You can enable it by passing -std=c99 to gcc.

As a side note, interestingly 9 is returned because it's the return of printf which just wrote 9 characters.

这篇关于为什么主要在这里不返回0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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