【发布时间】:2018-09-19 07:37:11
【问题描述】:
#include <stdio.h>
int main(void)
{
int a,b,c,t;
printf("Get the height of the triangle",a);
scanf("%d",&a);
printf("Get the base of the triangle",b);
scanf("%d",&b);
t=0.5;
c=t*(a*b);
printf("The area of the triangle:%d\n",c);
scanf("%d",&c);
return 0;
}
在编写代码、编译并执行之后,无论a和b的值是否输入,答案始终为零。我想知道为什么,以及如何纠正我的错误。
【问题讨论】:
-
int t; t = 0.5;在你心中是什么意思? -
好的,但是
t是一个整数。 -
t可以持有哪些值? -
int不是浮点类型。分配给int的任何浮点值都将在小数点处被截断并丢失小数点后的所有数字。所以,如果乘法的结果是0.xxxxxxxx,那么整数结果就是0。 -
@FredLarson:这实际上并不是问题。