The type of an integer is dependent on a complex combination of factors including:
- The magnitude of the constant;
- The implemented sizes of the integer types;
- The presence of any suffixes;
- The number base in which the value is expressed (i.e. decimal, octal or hexadecimal).
For example, the value 0x8000 is of type
unsigned int in a 16-bit environment, but of type (
int in a
- Any value with a "U" suffix is of unsigned type;
- An unsuffixed decimal value less than 2^31 is of signed type.
- An unsuffixed hexadecimal value greater than or equal to 2^15 may be of signed or unsigned type;
- For C90, an unsuffixed decimal value greater than or equal to 2^31 may be of signed or unsigned type.
In C++, if an overload set includes candidates for an
unsigned int and an
int, then the overload that would be matched by
0x8000 is therefore dependent on the implemented integer size. Adding a "U" suffix to the value specifies that it is unsigned.
- MISRA C:2004, 10.6 - A "U" suffix shall be applied to all constants of unsigned type.
- MISRA C++:2008, 2-13-3 - A "U" suffix shall be applied to all octal or hexadecimal integer literals of unsigned type.
- MISRA C:2012, 7.2 - A "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned type.