c中的递归函数不起作用

时间:2015-02-25 05:26:16

标签: c

    #include<stdio.h>
#include<math.h>
int power(int a)
{
    if(a<=0)
        return 1;
    else
        return((int)pow(5.0,a)+power(a--));
}
void main()
{
    printf("%d",power(2));
}

我使用上面的函数来处理我正在处理的更大的程序,但它无法在C中执行。请指出我的错误。

1 个答案:

答案 0 :(得分:3)

更改

return((int)pow(5.0,a)+power (a--));

return((int)pow(5.0,a)+power (a-1));

它不起作用的原因是a--返回a的当前值而不是a的递减值。所以你的递归函数永远不会结束,它是一个无限递归。