A simple integral constant always has an intrinsic type which is either int, unsigned int, long or unsigned long. It is not possible to define a constant of type char or short except by using an explicit cast. The type of a constant is determined by a number of factors:
The ISO C Standard (6.1.3.2) specifies rules for various types of constant which state that the type is determined from the first type in a list of types in which the value can be represented. So,
| FORMAT OF LITERAL |
POSSIBLE TYPES |
|---|---|
| Unsuffixed decimal | int, long, unsigned long |
| Unsuffixed octal or hexadecimal | int, unsigned int, long, unsigned long |
| Suffixed with u or U | unsigned int, unsigned long |
| Suffixed with l or L | long, unsigned long |
| Suffixed with both u/U and l/L | unsigned long |
The table below demonstrates how the type of an integer constant varies, depending on its magnitude and suffix. The size of an int is assumed to be either 16 bits or 32 bits as indicated.
Decimal / Hexadecimal |
Unsuffixed |
Unsuffixed |
Suffixed |
Suffixed |
Suffixed |
|---|---|---|---|---|---|
int: 16bits |
|
|
|
|
|
| 32767 / 0x7FFF | signed int | signed int | unsigned int | signed long | unsigned long |
| 32768 / 0x8000 | signed long | unsigned int | unsigned int | signed long | unsigned long |
| 65536 / 0x10000 | signed long | signed long | unsigned long | signed long | unsigned long |
| 2147483647 / 0x7FFFFFFF | signed long | signed long | unsigned long | signed long | unsigned long |
| 2147483648 / 0x80000000 | unsigned long | unsigned long | unsigned long | unsigned long | unsigned long |
int: 32bits |
|
|
|
|
|
| 32767 / 0x7FFF | signed int | signed int | unsigned int | signed long | unsigned long |
| 32768 / 0x8000 | signed int | signed int | unsigned int | signed long | unsigned long |
| 65536 / 0x10000 | signed int | signed int | unsigned int | signed long | unsigned long |
| 2147483647 / 0x7FFFFFFF | signed int | signed int | unsigned int | signed long | unsigned long |
| 2147483648 / 0x80000000 | unsigned long | unsigned int | unsigned int | unsigned long | unsigned long |
The type of a floating constant is determined solely by its suffix: