为什么输出(变量 - > s)为0? [英] Why is the output(variable -> s) 0?

查看:113
本文介绍了为什么输出(变量 - > s)为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>
int f(int);
int main()
{
    int n=12347,s=0;
    s=f(n);
    printf("S=%d\n",s);
    return 0;
}
int f(int a)
{
    int i,s=0,r;
    if(a==0)
    return s;
        r=a%10;
        s=r+s;
        f(a/10);
}

推荐答案

原因是你的所有功能都不是 f()显式返回一个值。



但是有一个(隐藏的)值返回:

你正在调用 f()作为代码中的最后一次调用,以便返回该函数调用的返回值。因为函数是迭代调用的,直到参数 a 为零,所以最后一次调用的返回值为零。然后将该值传递回每个下一个上限调用,以使最终返回值始终为零。





制作它更明白了。您的代码行为类似于:

The reason is that not all pathes of your function f() return a value explicitly.

But there is a (hidden) value returned:
You are calling f() as last call in your code so that the return value from that function call is returned. Because the function is called iteratively until the parameter a is zero, the return value of the last call is zero. Then that value is passed back to each next upper call so that the final return value is always zero.


To make it better understable. Your code behaves similar to:
return f(a/10);



返回整数类型的值(如 int )在特定的平台相关寄存器中传递。如果没有 return 语句,则寄存器仍包含上次调用的返回值。



如果您在上次通话中调用另一个函数,则值会有所不同:


Return values of integral type (like int) are passed in a specific platform dependant register. When not having a return statement, the register contains still the return value from the last call.

If you would have called another function as last call, the value would be different:

int f(int a)
{
    /* ... */
    f(a/10);
    printf("s=%d\n", s);
}



使用上述内容,返回 printf()函数的返回值(实际打印的字符数。)

[/ EDIT]


With the above, the return value of the printf() function is returned (the number of characters actually printed).
[/EDIT]


你的代码几乎可以做任何事情 - 当你从一个函数的结尾掉下来时输出一个显式的return语句,然后使用返回它的未定义行为的值。您的实现可以设置为几乎任何东西,仍然是一个有效的程序。如果它只是将整个函数转换为为每个代码路径返回0; 或者做一些更奇怪的事情,那么编译器将是完全合法的。



实际上你可能会得到任何通常用来返回整数值的寄存器(在x86和amd64的大多数编译器上通常是AX / EAX / RAX)。你可能幸运的是,之前的同一个寄存器用于整数除法。但是我真的不愿意继续这种行为。
Your code can do just about anything - in C when you fall off the end of a function with out an explicit return statement and then use the value that's returned it's undefined behaviour. Your implementation could set to just about anything and still be a valid program. The compiler would be perfectly legal if it just converted the entire function to return 0; for every code path or do something even weirder.

In reality you're probably getting whatever junk is left kicking around in whatever register is normally used to return integer values (usually AX/EAX/RAX on most compilers for x86 and amd64). You're probably "lucky" that the same register is used for an integer division immediately before. However I really wouldn't bank on that behaviour.


为什么要对这段代码感到烦恼?!修复它!

C ++标准明确指出这是未定义的行为



6.6.3:

[...]流出一个函数的末尾相当于一个没有值的返回;这导致值返回函数中的未定义行为。[...]



1.3.24:

未定义的行为 [...]允许的未定义行为包括完全用不可预测的结果忽略情况[...]



干杯

Andi



PS:C标准中的相应部分为6.8.6.4: [...]没有表达式的return语句只会出现在返回类型为void的函数中。[...]
Why bother about this code at all?! Fix it!
The C++ standard is explicitly stating this as undefined behavior:

6.6.3:
[...] Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.[...]

1.3.24:
undefined behavior [...] Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results[...]

Cheers
Andi

PS: The respective section in the C Standard is 6.8.6.4: [...] A return statement without an expression shall only appear in a function whose return type is void.[...]


这篇关于为什么输出(变量 - > s)为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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