unsigned int 和signed char 比较

发布于 2024-10-18 22:29:28 字数 218 浏览 1 评论 0原文

我试图将无符号 int 与有符号 char 进行比较,如下所示:

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

我期望 o/p 为“g”。相反,它是“s”。这里进行了什么样的转换?

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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤还要旧人安 2024-10-25 22:29:28

C99 的 6.3.1.8 节“常规算术转换” 详细介绍了隐式整数转换。

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

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

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

这不算数,因为一个已签名,另一个未签名。

否则,如果无符号整数类型的操作数的等级大于或等于另一个操作数类型的等级,则有符号整数类型的操作数将转换为无符号整数类型的操作数的类型。< /p>

宾果游戏。 x 的等级高于 y,因此 y 被提升为 unsigned int。这意味着它从 -1 转变为 UINT_MAX,远大于 9。

其余规则不适用,因为我们已经找到了匹配项,但我将包括为了完整性:

否则,如果有符号整型操作数的类型可以表示无符号整型操作数类型的所有值,则将无符号整型操作数转换为有符号整型操作数的类型类型。

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


与此问题相关的排名如下所示。所有排名均在 C99 的 6.3.1.1 部分、布尔值、字符和整数 中详细介绍,因此您可以参考该内容了解更多详细信息。

long long int的rank应大于long int的rank,即
应大于int的等级,后者应大于short int的等级,后者应大于signed char的等级>.

char 的等级应等于signed charunsigned char 的等级。

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.

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:

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.


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.

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.

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

甲如呢乙后呢 2024-10-25 22:29:28

我的猜测是 y 被提升为 unsigned int ,这成为一个很大的值(由于包装)。因此条件满足。

My guess is y is promoted to unsigned int which becomes a big value (due to wrapping). Hence the condition is satisfied.

无声静候 2024-10-25 22:29:28

运行以下代码:

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

输出为:

4294967295
s

转换后,y 取一个非常大的值。这就是为什么输出是s。

Ran the following code:

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

The output is:

4294967295
s

After a casting, y takes a very large value. That is why output is s.

↙温凉少女 2024-10-25 22:29:28

char 被提升为 unsigned int,值为 MAX_UINT,大于 9。

The char is promoted to unsigned int, with a value of MAX_UINT, which is greater than 9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文