我一直试图在将 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/