Possible Duplicate:
What is really happening in this code?
我有一个包含递归函数的代码。我在递归上浪费了很多时间,但我仍然无法得到它,真的:
#include<stdio.h>
count(int);
main(){
int x=10,z;
z=count(x);
}
count(int m){
if(m>0)
return count(m-1);
}
何时 count
第一次使用参数 10
调用,满足条件并开始递归。当函数调用自身时到底会发生什么?我不明白。声明 return count(m-1)
是什么意思?意思是?它把控制权转移到哪里?
最佳答案
函数的返回值count
未定义,因为 if (m <= 0)
没有默认返回是真的。
C11, § 6.9.1 Function definitions
If the
}
that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
此外,要了解递归函数的工作原理,您必须拿一篇论文并尝试自己执行代码(另请参阅 here )。
关于c - 这段代码给出了一个荒谬的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12265994/