除法结果始终为零 [英] Division result is always zero
本文介绍了除法结果始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到这个C $ C $℃。
I got this C code.
#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;
}
我想知道为什么我的变量T始终为零(在printf函数)?
I want to know why my variable 't' is always zero (in the printf function) ?
推荐答案
因为在这个前pression
because in this expression
t = (1/100) * d;
1和100是整数值,整数除法截断,所以这这是相同的,因为这
1 and 100 are integer values, integer division truncates, so this It's the same as this
t = (0) * d;
你需要做一个恒定的浮动这样
you need make that a float constant like this
t = (1.0/100.0) * d;
您可能还希望做同样与此
you may also want to do the same with this
k = n / 3.0;
这篇关于除法结果始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文