为什么main在这里不返回0?
我刚刚阅读了
ISO/IEC 9899:201x 委员会草案 - 2011 年 4 月 12 日
,其中我在 5.1.2.2.3 程序终止下发现,
..reaching the } that terminates the main function returns a value of 0.
这意味着如果您未在 main()
,如果程序运行成功,那么main的右大括号}处会返回0。
但是在下面的代码中我没有指定任何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);
}
编译
gcc test.c
./a.out
sum is 15
echo $?
9 // here it should be 0 but it shows 9 why?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该规则是在 1999 年版本的 C 标准中添加的。在C90中,返回的状态是未定义的。
您可以通过将
-std=c99
传递给 gcc 来启用它。顺便说一句,有趣的是,返回了 9,因为它是
printf
的返回,它只写了 9 个字符。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.它返回
printf
的返回值,即实际打印的字符数。It returns return value of
printf
which is number of characters really printed out.函数的返回值通常存储在CPU的eax寄存器中,因此语句“return 4;”通常会编译
并返回 x (取决于您的编译器)类似于:
如果您不指定返回值,那么编译器仍会吐出“ret”,但不会更改 eax 的值。所以调用者会认为之前留在 eax 寄存器中的就是返回值。对于这个例子,它通常是返回值 printf,但是不同的编译器会生成不同的机器代码并以不同的方式使用某些寄存器。
这是一个简化的解释,不同的调用约定和目标平台将发挥至关重要的作用,但它应该有足够的信息来解释示例中“幕后”发生的情况。
如果您对汇编程序有基本的了解,那么值得比较不同编译器的反汇编。您可能会发现某些编译器会清除 eax 寄存器作为保护措施。
The return value from a function is normally stored in the eax register of the cpu, so the statement "return 4;" would usually compile to
and return x (depending on your compiler) would be something like:
if you don't specify a return value then the compiler will still spit out the "ret" but doesn't change the value of eax. So the caller will think that what ever was left in the eax register previously is the return value. For this example it would usually be the return value printf, but different compilers will generate different machine code and use some registers differently.
This is a simplified explanation, different calling conventions and target platforms will play a vital role but it should be enough information to explain what is happening 'behind the scenes' in your example.
If you've got a basic understanding of assembler it's worth comparing the disassembly of different compilers. You may find that some compilers are clearing the eax register as a safeguard.