为什么0XAA是unsigned int而不是int?

时间:2016-11-18 12:24:02

标签: c

我是从 C Primer Plus,第6版,第3章的复习题中看到的。

问题:

Question Picture

附录A中的答案:

Answer Picture

注意d.0XAA,我的答案是int常数,十六进制格式,但答案是unsigned int

我想知道为什么

1 个答案:

答案 0 :(得分:5)

那本书不对。根据C11 6.4.4.1,十六进制的整数常量的类型由该表确定:

Suffix    ...    Octal or Hexadecimal Constant

None      ...    int
                 unsigned int
                 long int
                 unsigned long int
                 long long int
                 unsigned long long int

u or U    ...    unsigned int
                 unsigned long int
                 unsigned long long int

您的常量0xAA没有后缀,因此上表的上半部分适用。含义:编译器将首先检查该值是否适合int。如果它不合适,它将检查它是否适合unsigned int等等。

在C的任何已知实现上,值0xAA肯定适合int。该问题的正确答案是int

但是,如果常量为0xAAu,引用表的底部部分将会应用,结果将为unsigned int