An “integer constant” is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed.
integer-constant :
decimal-constant integer-suffix opt
octal-constant integer-suffix opt
hexadecimal-constant integer-suffix opt
decimal-constant :
nonzero-digit
decimal-constant digit
octal-constant :
0
octal-constant octal-digit
hexadecimal-constant :
0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit
nonzero-digit : one of
1 2 3 4 5 6 7 8 9
octal-digit : one of
0 1 2 3 4 5 6 7
hexadecimal-digit : one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix :
unsigned-suffix long-suffix opt
long-suffix unsigned-suffix opt
unsigned-suffix : one of
u U
long-suffix : one of
l L
Integer constants are positive unless they are preceded by a minus sign (–). The minus sign is interpreted as the unary arithmetic negation operator. (See “Unary Operators” for information about this operator.)
If an integer constant begins with the letters 0x or 0X, it is hexadecimal. If it begins with the digit 0, it is octal. Otherwise, it is assumed to be decimal.
The following lines are equivalent:
0x1C /* = Hexadecimal representation for decimal 28 */
034 /* = Octal representation for decimal 28 */
No white-space characters can separate the digits of an integer constant. These examples show valid decimal, octal, and hexadecimal constants.
/* Decimal Constants */
10
132
32179
/* Octal Constants */
012
0204
076663
/* Hexadecimal Constants */
0xa or 0xA
0x84
0x7dB3 or 0X7DB3
Every integer constant is given a type based on its value and the way it is expressed. You can force any integer constant to type long by appending the letter l or L to the end of the constant; you can force it to be type unsigned by appending u or U to the value. The lowercase letter l can be confused with the digit 1 and should be avoided. Some forms of long integer constants follow:
/* Long decimal constants */
10L
79L
/* Long octal constants */
012L
0115L
/* Long hexadecimal constants */
0xaL or 0xAL
0X4fL or 0x4FL
/* Unsigned long decimal constant */
776745UL
778866LU
The type you assign to a constant depends on the value the constant represents. A constant's value must be in the range of representable values for its type. A constant's type determines which conversions are performed when the constant is used in an expression or when the minus sign (–) is applied. This list summarizes the conversion rules for integer constants.
The type for a decimal constant without a suffix is either int, long int, or unsigned long int. The first of these three types in which the constant's value can be represented is the type assigned to the constant.
The type assigned to octal and hexadecimal constants without suffixes is int, unsigned int, long int, or unsigned long int depending on the size of the constant.
The type assigned to constants with a u or U suffix is unsigned int or unsigned long int depending on their size.
The type assigned to constants with an l or L suffix is long int or unsigned long int depending on their size.
The type assigned to constants with a u or U and an l or L suffix is unsigned long int.
The limits for integer types are listed in Table 1.3. These limits are also defined in the standard header file LIMITS.H.Table 1.3 Limits on Integer Constants
Constant | Meaning | Value |
CHAR_BIT | Number of bits in the smallest variable that is not a bit field. | 8 | |
SCHAR_MIN | Minimum value for a variable of type signed char., -127 | ||
SCHAR_MAX | Maximum value for a variable of type signed char., 127 | ||
UCHAR_MAX | Maximum value for a variable of type unsigned char. | 255 (0xff) | |
CHAR_MIN | Minimum value for a variable of type char., Same as -127; 0 if /J option used. | ||
CHAR_MAX | Maximum value for a variable of type char., Same as 127; 255 if /J option used. |
Table 1.3 Limits on Integer Constants (continued)
Constant | Meaning | Value |
MB_LEN_MAX | Maximum number of bytes in a multicharacter constant., 2 | ||
SHRT_MIN | Minimum value for a variable of type short., -32767 | ||
SHRT_MAX | Maximum value for a variable of type short., 32767 | ||
USHRT_MAX | Maximum value for a variable of type unsigned short. | 65535 (0xffff) | |
INT_MIN1 | Minimum value for a variable of type int. | -32767 | |
INT_MAX2 | Maximum value for a variable of type int. | 32767 | |
UINT_MAX3 | Maximum value for a variable of type unsigned int. | 65535 (0xffff) | |
LONG_MIN | Minimum value for a variable of type long., -2147483647 | ||
LONG_MAX | Maximum value for a variable of type long., 2147483647 | ||
ULONG_MAX | Maximum value for a variable of type unsigned long. | 4294967295 (0xffffffff) |
1 The value for INT_MIN is–2147483648 for 32-bit target compilations.
2 The value for INT_MAX is 2147483647 for 32-bit target compilations.
3 The value for UINT_MAX is 4294967295 (0xffffffff)for 32-bit target compilations.
If a value exceeds the largest integer representation, the Microsoft compiler generates an error.¨