我不明白为什么如果将所有 double
更改为 int
就可以正常工作,
我的 printf 语句有问题吗?
您使用 %f
或 %lf
表示 double
,对吧?
/*
double power(double a, int b) that calculates the power ab. You are allowed to use the
multiplication operator *.
*/
#include <stdio.h>
#include <conio.h>
double power(double a,int b)
{
double buffer;
if(b > 1)
{
buffer = a * power(a,b-1);
return buffer;
}
else
return a;
}
int main(void)
{
double a;
int b;
int buffer;
scanf("%f",&a);
scanf("%d",&b);
buffer = power(a,b);
printf("%f",buffer);
getch();
}
最佳答案
int buffer; printf("%f",buffer);
您在 printf 中使用了错误的说明符,这是一个严重的问题,因为 printf
无法转换参数。使用 %d
或将 buffer
更改为 double 。
作为第二个问题,您还需要在 scanf
中使用 %lf
而不是 %f
,因为 a
是一个 double
,而不是 float
。
关于c - Double 在递归中返回 0,但在 int 时工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12235222/