整型与浮点型数据运算结果为什么是0?
# include <stdio.h>int main(void)
{
float sum = 0;
int i = 1;
printf("%f\n", 1/i); //此时输出的值为0,为啥?
printf("%d\n",sum+i); //此时输出的值为0,为啥?
for (i=1; i<=100; i++)
{
// sum = sum + 1/i; //最终sum为0,为啥;
sum = sum + (float)(1/i); // 此处虽然进行了强制类型转换,但是sum的值还是为0,为啥?
}
printf("%d\n", sum);
return 0;
}