0

What is by default type of integral literal defined below:

0X123 /* hex , int ? unsigned int? long? unsigned long? */
0XFFFFFFFE /* hex , value is (2^32-2)=4294967294 .*/ 
0123 /*octal */ /* value = 83*/
042747672777 /* octal , greater than 2^32*/ /* value=4691293695 */

I read in some tutorial or book (I don't remeber source) that they are by default of signed int type. Is that correct?

foxtrot9
  • 324
  • 5
  • 14

1 Answers1

4

The type of the integral literal is defined less by whether it is a hexadecimal literal, a decimal literal, or an octal literal and more by the value of the literal.

Table 6 — Types of integer constants, in Section 2.14.2 of the C++11 Standard lists the order of types that will be used to capture an integral literal.

The main difference between decimal literals and hexadecimal and octal literals is that the order of types of decimal literals is int, long, long long while the order of types of hexadecimal and octal literals is int, unsigned int, long, unsigned long, long long, and unsigned long long.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • that means `0X123` is int , `0X4294967294` (2^32 -2 ) is unsigned int , `0X4294967297` (2^32 + 1) is long , `0123` is int , `042747672777` is long. (considering int of 32 bits and long of 64 bit.) – foxtrot9 Aug 05 '16 at 07:02
  • @foxtrot9 You are confusing the use of the `0` and `0x` prefix. `0X4294967294` is certainly not `2**32 - 2`, it something much larger. `0x123 != 123`. – Holt Aug 05 '16 at 07:05
  • @Holt , Oh I forgot. I will change that. – foxtrot9 Aug 05 '16 at 07:06
  • 1
    @foxtrot9, barring the small errors in your comment, you are thinking along the right lines. – R Sahu Aug 05 '16 at 07:07
  • @M.M , sorry for mistake. edited to reflect that. – foxtrot9 Aug 05 '16 at 07:08
  • that means 0X123 is int , 0XFFFFFFFE (2^32 -2 ) is unsigned int , 0X100000001 (2^32 + 1) is long , 0123 is int , 042747672777 is long. (considering int of 32 bits and long of 64 bit.) .......... Right? – foxtrot9 Aug 05 '16 at 07:09
  • @foxtrot9 you still make the same error with 0x429... – M.M Aug 05 '16 at 07:10
  • 1
    @foxtrot9 For 32-bits `int` and 64-bits `long`, values up to `2^31-1` (included) will be `int` (whatever the prefix), values between `2^31` and `2^32-1` (included) will be `long` without prefix and `unsigned int` with hexadecimal or octal prefix, values between `2^32` and `2^63-1` will be `long` whatever the prefix, etc. – Holt Aug 05 '16 at 07:17