c - 在c中将 float 除以整数时得到整数结果

标签 c integer division

我一直试图在将 float 除以整数时得到一个整数值,但我无法想出我要找的东西。

int a = 3 ;
float b = 15.50 ;
int result ;
result = int b/a

我已经很长时间没有编程了,我知道这是基本的,我应该意识到这一点,我非常感谢任何帮助。谢谢

最佳答案

编译器自动将类型转换为赋值运算符左侧操作数的类型。在本例中,它的类型为 int,因此结果将隐式转换为 int。您不必显式地进行类型转换

 #include<stdio.h>

 int main()
 {    
        int a = 3 ;
        float b = 15.50 ;
        int result ;
        result = b/a;  // automatic type promotion of a to float so b/a = 5.1666666666
                       //but due to implicit type casting 5.16666 truncated to 5
        printf("%d",result);   // 5 will be printed.
        return 0;
 }

关于c - 在c中将 float 除以整数时得到整数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20411498/

相关文章:

java - Java中的除法以获得下一个更高的值

python - 如果大小增加,为什么 python 列表的位置没有改变?

c - 格式化 printf 和额外的字符串打印?

c - 使用信号和 sigpipe

mysql - 将日期转换为 int

python - 使用 python3.x 在 numpy 中进行楼层划分的 ufunc 是什么?

c - 如何将数据插入到 MCU 编译的二进制文件中

将两个整数合并为 1?

java - Java中是否有内置方法可以将字符串转换为整数数组?

ruby - 为什么我不能将一个 fixnum 除以另一个 fixnum?