34

Does C treat hexadecimal constants (e.g. 0x23FE) as signed or unsigned integers?

2

3 Answers 3

32

The number itself is always interpreted as a non-negative number. Hexadecimal constants don't have a sign or any inherent way to express a negative number. The type of the constant is the first one of these which can represent their value:

int
unsigned int
long int
unsigned long int
long long int
unsigned long long int
8
  • 2
    Note that as a consequence, 0x8000 may be either signed or unsigned depending on whether sizeof(int) is 2 or 4. Yuck! Just append u if you really need unsigned.
    – anatolyg
    Jan 19, 2011 at 17:15
  • 2
    @anatolyg: I'm not sure what you mean by "yuck". It will always be positive and it will always convert to the correct value if assigned or promoted to another type where the value is still in range which seems like fairly sensible and desirable behaviour to me.
    – CB Bailey
    Jan 19, 2011 at 17:18
  • 2
    @anatolyg: But 0x8000 isn't negative. Either it can fit in an int, in which case 0x8000 > 0x7000 is done as a comparison of int, otherwise 0x8000 is an unsigned and 0x7000 is promoted to unsigned (no change of value) and the comparison is a comparison of unsigned. Either way the result is true.
    – CB Bailey
    Jan 19, 2011 at 20:35
  • 2
    Decimal and octal constants don't have a sign either - if you write -1, you're writing a unary - followed by a decimal constant 1. In @anatolyg's example, if (MYSIZE > -1) could produce surprising results, since the -1 may or may not be promoted to unsigned.
    – caf
    Jan 20, 2011 at 0:20
  • 1
    @caf: 0x8000 > -1 is a much better example of where care definitely is needed.
    – CB Bailey
    Jan 20, 2011 at 7:50
15

It treats them as int literals(basically, as signed int!). To write an unsigned literal just add u at the end:

0x23FEu
4
  • 5
    I don't think that you can leave that statement as such. E.g provided that the width of int is 32 bit the value 0x8000 is unsigned (namely INT_MAX + 1) and not signed (and INT_MIN). Jan 19, 2011 at 16:40
  • 3
    @JensGustedt: Presumably you mean that if the width of int is 16 bit then 0x8000 will be unsigned?
    – CB Bailey
    Jan 19, 2011 at 16:43
  • 1
    @Charles, probably. Counting bits myself never was my strength :) Jan 19, 2011 at 16:56
  • 2
    @Alex, no. A hexadecimal value is int as long as the value fits into int and for larger values it is unsigned, then long, then unsigned long etc. See Section 6.4.4.1 of the C standard. Just as the accepted answer states. Apr 2, 2020 at 13:26
5

According to cppreference, the type of the hexadecimal literal is the first type in the following list in which the value can fit.

int
unsigned int
long int
unsigned long int
long long int(since C99)
unsigned long long int(since C99) 

So it depends on how big your number is. If your number is smaller than INT_MAX, then it is of type int. If your number is greater than INT_MAX but smaller than UINT_MAX, it is of type unsigned int, and so forth.

Since 0x23FE is smaller than INT_MAX(which is 0x7FFF or greater), it is of type int.

If you want it to be unsigned, add a u at the end of the number: 0x23FEu.

2
  • This answer appears to pertain to C++, not C. It may be correct, but I think the reference link should be updated to point to the C reference, not the C++ reference. Sep 13, 2018 at 0:25
  • 4
    @RadonRosborough I've updated the answer to use the C reference and checked the other parts of it to make sure the answer was all about C. Thanks for pointing it out.
    – Searene
    Sep 13, 2018 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.