Is the literal 0xffffffff int or unsigned in C++

为君一笑 提交于 2019-12-05 09:18:15

问题


According to this, integer literals without type suffix are always ints. However, both gcc and clang interpret 0xffffffff (or any literal which explicitly sets the sign bit other than using the -) as unsigned. Which is correct? (according to this the compilers are)


回答1:


Per Paragraph 2.14.2/2 of the C++11 Standard,

The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented.

Table 6 reports that for hexadecimal constants, the type should be:

  • int; or (if it doesn't fit)
  • unsigned int; or (if it doesn't fit)
  • long int; or (if it doesn't fit)
  • unsigned long int; or (if it doesn't fit)
  • long long int; or
  • unsigned long long int.

Assuming your implementation has 32-bit int, since 0xffffffff does not fit in an int, its type should be unsigned int. For an implementation with a 64-bit int, the type would be int.

Notice, that if you had written the same literal as a decimal constant instead, the type could have only been:

  • int; or (if it doesn't fit)
  • long int; or (if it doesn't fit)
  • long long int.


来源:https://stackoverflow.com/questions/15744310/is-the-literal-0xffffffff-int-or-unsigned-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!