有人可以解释一下为什么这段代码返回0?
#include <stdio.h>
int factorial(int input)
{
if (input > 0)
{
input--;
return input * factorial(input);
}
return 1;
}
int main()
{
printf("%d", factorial(20));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于最后一次入站迭代,在代码中,当input
为1时,执行
if (input > 0)
{
input--; // see here, 1 goes to 0.....
return input * factorial(input);
}
Run Code Online (Sandbox Code Playgroud)
基本上给你
return 0 * factorial (0);
Run Code Online (Sandbox Code Playgroud)
最终将整个返回值设为0.
归档时间: |
|
查看次数: |
54 次 |
最近记录: |