According to this, integer literals without type suffix are always int
s. 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)
Asked
Active
Viewed 3,042 times
25

Suvarna Pattayil
- 5,136
- 5
- 32
- 59

Walter
- 44,150
- 20
- 113
- 196
-
I don't know, but I'm interested to know why it matters. – Roger Rowland Apr 01 '13 at 13:23
-
@roger_rowland it doesn't, presumably, but clang warns me about a conversion from unsigned to signed, which is annoying and which I didn't understand – Walter Apr 01 '13 at 13:32
-
I guess the point is 0xffffffff = 4294967295 are positive numbers and do not fit in an int. – brian beuning Apr 01 '13 at 13:55
-
`std::is_signed
::value` will tell you. – Pubby Apr 01 '13 at 17:09 -
@Pubby I know what the compiler thinks. I wanted to know what the rules say. – Walter Apr 01 '13 at 17:15
1 Answers
25
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
; orunsigned 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
.

Andy Prowl
- 124,023
- 23
- 387
- 451
-
Interesting. Would you please link the quote to its online web-page, if it's possible? – masoud Apr 01 '13 at 13:32
-
7@MM.: This is the C++11 Standard (I am using Draft 3485, which is slightly more recent than the official Standard). The Draft can be downloaded for free from [here](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3485.pdf). The official Standard is paid. – Andy Prowl Apr 01 '13 at 13:33