微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用条件运算符比较两个无符号值时的有符号/无符号不匹配

我有以下C代码
unsigned int a;
unsigned char b,c;
void test(void) {
    if (a < b)
        return;
    if (a < (b ? b : c))
        return;
}

当我编译它(使用Microsoft cl,来自MS SDK 7,-W3警告级别)时,第二次比较会发出警告:C4018,签名/无符号不匹配.第一次比较没有发出警告.

我已经检查了MS docs on the conditional operator并且他们说如果两个操作数是相同的类型,结果将是相同的类型,所以它应该作为第一个比较.我错过了什么吗?

UPD:用gcc -Wall -Wextra -pedantic进行测试,没有任何警告.

解决方法

这可能是由于算术转换规则:首先,任何整数类型的转换等级小于int(例如unsigned char)将提升为int或unsigned int.

结果是int还是unsigned int不会(直接)依赖于原始类型的签名,但是它的范围:int甚至用于无符号类型,只要可以表示所有值,这就是unsigned char的情况.在主流架构上.

其次,由于两个操作数最终具有相同的转换等级,但是一个是无符号的,另一个操作数也将转换为无符号类型.

在语义上,您的表达式读取

a < (unsigned int)(int)b

a < (unsigned int)(b ? (int)b : (int)c)

编译器显然足够聪明,可以注意到第一种情况不会导致问题,但第二种情况则失败.

Steve Jessop的评论很好地解释了这可能发生的原因:

I would imagine that in the first case the compiler thinks,“I have a comparison operator whose operand types are unsigned int and unsigned char. No need for a warning,Now let’s apply promotion followed by usual conversion”.

In the second case it thinks,“I have a comparison operator whose operand types are unsigned int and int (which I derived as the type of the conditional expression on the RHS). Best warn about that!”.

原文地址:https://www.jb51.cc/c/119646.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐