unsigned int类型和符号字符比较 [英] unsigned int and signed char comparison

查看:168
本文介绍了unsigned int类型和符号字符比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这样一个符号的字符比较一个unsigned int:

I am trying to compare an unsigned int with a signed char like this:

int main(){
  unsigned int x = 9;
  signed char y = -1;
  x < y ? printf("s") : printf("g");
  return 0;
}

我期待的O / P为G。相反,它的S。什么样的转换在这里做什么?

I was expecting the o/p to be "g". Instead, its "s". What kind of conversion is done here?

推荐答案

6.3.1.8 常见的算术转换,C99的的细节隐整数转换。

Section 6.3.1.8, Usual arithmetic conversions, of C99 details implicit integer conversions.

如果两个操作数具有相同的类型,则不需要进一步转化

If both operands have the same type, then no further conversion is needed.

这不算数,因为他们是不同类型的。

That doesn't count since they're different types.

否则,如果两个操作数有符号整数类型或两者都有的无符号整数类型,具有较小的整数转换等级的类型将被转换为操作数的类型有更大的等级。

Otherwise, 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.

因为一个不计签订后,其他未签名。

That doesn't count since one is signed, the other unsigned.

否则,如果具有无符号整型操作数的秩大于或等于另一个操作数的类型的秩,然后用符号整型操作数被转换成无符号整型操作数的类型。

Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

宾果。 X Ÿ较高的排名桑尼被提升到 unsigned int类型。这意味着,从 1 摇身一变 UINT_MAX ,大大高于9。

Bingo. x has a higher rank than y so y is promoted to unsigned int. That means that it morphs from -1 into UINT_MAX, substantially larger than 9.

的其余规则不适用,因为我们发现我们的比赛,但我会包括他们的完整性:

The rest of the rules don't apply since we have found our match but I'll include them for completeness:

否则,如果有符号整数类型的操作数的类型可以重新present所有无符号整型操作数的类型的值,然后用无符号整型的操作数转换为的类型操作有符号整数类型。

Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

否则,两个操作数转换为对应于与符号整型操作数的类型的无符号整型

Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

有关这个问题的行列如下所示。所有等级在C99详细说明,部分 6.3.1.1 布尔,字符和整数的,所以你可以参考,对于进一步的细节。


The ranks relevant to this question are shown below. All ranks are detailed in C99, section 6.3.1.1, Boolean, character, and integers so you can refer to that for further details.

得到long long int的列应大于长整型,军衔其中较大
  应大于 INT 军衔,这应该比军衔短的大于
  INT
,它应该比符号字符

The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

字符的等级应等于符号字符 unsigned char型<军衔/ code>。

The rank of char shall equal the rank of signed char and unsigned char.

这篇关于unsigned int类型和符号字符比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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