(float)9/5只投9次而不是9/5的结果。所以/操作在这一点上不是一个整数操作。 (浮动)(9/5)将给出1.00000的结果。
尝试编译所有警告,它可能会告诉你很多错误。
例如一个64位的Linux系统上,用gcc编译我-Wall得到:
pf.c: In function ‘main’:
pf.c:6:2: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat]
pf.c:6:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat]
pf.c:6:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 5 has type ‘long unsigned int’ [-Wformat]
正如上面提到的,你需要正确的格式说明。使用正确的格式说明和铸造给出:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("%f %f %lu %lu\n",(float)(9/5),(float)4,sizeof(float),sizeof(int));
return 0;
}
给出:
[email protected]:~/proj/c=>./pf
1.000000 4.000000 4 4
@Shahbaz现在,它不是标签C++了,真 – MrRap