其他分享
首页 > 其他分享> > c – 将double与int进行比较

c – 将double与int进行比较

作者:互联网

在阅读“使用C编程原理和实践”一书时,在第3章中我们可以直接将double与int进行比较.但是,当我在Visual Studios上测试它时,它运行正常,没有错误?他不能将double与int进行比较是什么意思.后来,他解释说C提供了一种间接的方式.他是指隐含转换吗?

解决方法:

C有一组在[over.built]中定义的内置运算符.相等运算符的行为在[expr.eq]中定义,特别是:

07002 If both operands are of arithmetic or enumeration type, the 07003 are performed on both operands; each of the operators shall yield true if the specified relationship is true and false if it is false.

并且usual arithmetic conversions意味着:

07005 Otherwise, if either operand is double, the other shall be converted to double.

因此,如果将int与float,double或long double进行比较,则会得到从int到float,double或long double的隐式转换.

标签:c,comparison,type-safety
来源: https://codeday.me/bug/20190823/1697486.html