尤达条件和整数促销 [英] Yoda Conditions and integer promotion

查看:147
本文介绍了尤达条件和整数促销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在比较类型比 INT 幅度更大,在整型常量,我应该在左边放置或以确保正确的比较正确的常数进行的?

When comparing a type larger than int, with an integer constant, should I place the constant on the left or the right to ensure the correct comparison is performed?

int64_t i = some_val;
if (i == -1)

或者它应该是:

if (-1 == i)

是否有任何情况下,在这两种情况下是不相同的比较 -1LL (其中的int64_t 长长)?

推荐答案

这不要紧,你是否把它放在右侧或左侧;在 == 运营商是完全对称的。

It doesn't matter whether you put it on the right hand side or the left hand side; the == operator is completely symmetrical.

如果两个操作数的 == 运营商有算术类型,在这种情况下,那么通常的算术转换应用(C99§6.5.9)。在这种情况下,应用该规则是:

If both operands to the == operator have arithmetic type, as in this case, then the "usual arithmetic conversions" are applied (C99 §6.5.9). In this case, the rule that applies is:

如果两个操作数有符号整数类型或都有签名
  整数类型,具有较小的整数转换等级类型的操作数
  转换为操作数的更大级别的类型。 (C99§6.3.1.8)

If both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank. (C99 §6.3.1.8)

所以-1转换为的int64_t -1LL 没什么区别。

So the -1 is converted to int64_t. -1LL makes no difference.

这篇关于尤达条件和整数促销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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