为什么我的花车被截断了? [英] Why did my float get truncated?

查看:63
本文介绍了为什么我的花车被截断了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include <stdio.h>

 int main(void)
 {
     float c =8/5;
     printf("The Result: %f", c);
     return 0;
 }

答案是 1.000000 .为什么不是 1.600000 ?

推荐答案

C会将您的 8/5 输入解释为整数.对于整数,C会将其截断为1.

C is interpreting your 8/5 input as integers. With integers, C truncates it down to 1.

将代码更改为 8.0/5.0 .这样,它就知道您正在使用实数,并且它将存储您要查找的结果.

Change your code to 8.0/5.0. That way it knows you're working with real numbers, and it will store the result you're looking for.

这篇关于为什么我的花车被截断了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆