我得到了这个C代码.
#include <stdio.h>
int main(void)
{
int n, d, i;
double t=0, k;
scanf("%d %d", &n, &d);
t = (1/100) * d;
k = n / 3;
printf("%.2lf\t%.2lf\n", t, k);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我的变量't'总是为零(在printf函数中)?
可能重复:
C中整数除法的行为是什么?
当我使用应该在1和0之间的答案进行除法计算时,它总是返回0.
试试这个:
float a = 1;
float b = 2;
float c = a / b; //variable divide by variable gives 0.5 as it should
float d = 1 / 2; //number divide by number gives 0
float e = 4 / 2;
NSLog(@"%f %f %f %f %f", a,b,c, d, e);
Run Code Online (Sandbox Code Playgroud)
我从控制台得到这个:
2013-01-17 14:24:17.512 MyProject [1798:c07] 1.000000 2.000000 0.500000 0.000000 0.500000
我不知道发生了什么事.