c - 当使用比较运算符比较具有相同值的 int 和 float 变量时会发生什么?
问题描述
当使用比较运算符比较具有相同值的 int 和 float 变量时会发生什么?
main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;
}
output : x and y are equal
What happens when x is compared with y variable?
解决方案
被隐式int
转换为类型;你的代码相当于float
if ((float)x == y)
请注意,即使从 aint
到 a的转换float
失去精度(在您的情况下不会),也会发生这种情况。
推荐阅读
- c++ - 从模板类中提取模板
- python - pydub.playback.play() 不播放声音
- c# - 如何不将字符串实例与换行符匹配?
- cs50 - 如何正确地重新提示 mario.c 中的语句?
- azure-pipelines - 限制从 Azure DevOps 管道中进行全局代理计算机更改的能力
- django - 如何在 Django 中将查询集过滤到当前用户
- r - 巨型 csv -> R 中的大量 csv
- javascript - MapBox 网络应用地图小部件,多个“绘制”参数。添加不透明度和半径失败
- javascript - 在三元操作中使用 React JSX 和 HTML 无法正确显示
- python - 来自数据框的嵌套饼图