为什么要分两个整数没有得到一个浮动? [英] Why dividing two integers doesn't get a float?

查看:160
本文介绍了为什么要分两个整数没有得到一个浮动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么B获得在这里圆满结束时,我用一个整数除以它,虽然它是一个浮?

Can anyone explain why b gets rounded off here when I divide it by an integer although it's a float?

#include <stdio.h>

void main() {
    int a;
    float b, c, d;
    a = 750;
    b = a / 350;
    c = 750;
    d = c / 350;
    printf("%.2f %.2f", b, d);
    // output: 2.00 2.14
}

HTTP://$c$cpad.org/j1pckw0y

推荐答案

这是因为隐式转换。变量 B,C,D 浮动键入。但 / 操作员可以看到两个整数它来划分,因此在被隐式转换为浮动通过加入小数点。如果你想浮动师,尝试使两个操作数的 / 浮动。就像如下:

This is because of implicit conversion. The variables b, c, d are of float type. But the / operator sees two integers it has to divide and hence returns an integer in the result which gets implicitly converted to a float by the addition of a decimal point. If you want float divisions, try making the two operands to the / floats. Like follows.

#include <stdio.h>

int main() {
    int a;
    float b, c, d;
    a = 750;
    b = a / 350.0f;
    c = 750;
    d = c / 350;
    printf("%.2f %.2f", b, d);
    // output: 2.14 2.14
    return 0;
}

这篇关于为什么要分两个整数没有得到一个浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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