我收到了我的下属的一个问题,但是我无法解决。以下是他从Code::Blocks官方网站下载的IDE中使用的代码。
这是一个hello world控制台项目,他只是稍微修改了一下,使用了头文件math.h
和pow()
函数。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Hello world! %d\n",pow(2,2));
return 0;
}
这段代码的输出应该是
Hello world! 4
,对吧?但是,实际上总是Hello world! 0
,除非使用printf("Hello world! %f\n", pow(2,2));
,这样语法上没问题,也是正确的做法。但接下来就变成了另一回事。Pow函数应该返回一个值为4的
double
类型。那么到底发生了什么呢?printf()
不起作用了,或者pow()
存在某些问题。
double
,因此您必须将其转换为(int)
。 - Rizier123%d
格式与double
值之间的不匹配。 - Jonathan Leffler