Integer literals may be expressed in a variety of formats. The sections below describe the rules for each format and provide examples.
Decimal (base 10)
Rules
- Cannot start with 0 (except for 0 itself) - that would make it octal
- Cannot include a decimal point - that would make it a floating point, even if the fractional part is 0
- Cannot include commas or spaces
- Must use only the digits 0-9
- May be preceded by a minus sign "-"
Examples of Valid Decimal Integers
Examples of Invalid Decimal Integers
Hexadecimal (base 16)
Rules
- Must begin with 0x or 0X
- May use digits 0-9 and the letters A-F or a-f (not case sensitive)
- Cannot include a decimal point
- Cannot include commas or spaces
- May be preceded by a minus sign "-"
Examples of Valid Hexadecimal Integers
Yes, 0x is legal and represents a value of 0. However, nobody uses 0x alone, except perhaps in The International Obfuscated C Code Contest.
Examples of Invalid Hexadecimal Integers
Octal (base 8)
While octal is still part of the ANSI/ISO C specification, almost no one uses it anymore.
Rules
- Must begin with 0
- Must use only the digits 0-7
- Cannot include a decimal point
- Cannot include commas or spaces
- May be preceded by a minus sign "-"
Examples of Valid Octal Integers
Examples of Invalid Octal Integers
Binary (base 2)
WarningCaution: ANSI C does not specify a format for binary integer literals. However, this quasi-standard notation is supported by many compilers for embedded microcontrollers. If your code must be ANSI compliant, do not use this notation.
Rules
- Must begin with 0b or 0B
- Must use only the digits 0-1
- Cannot include a decimal point
- Cannot include commas or spaces
- May be preceded by a minus sign "-"
Examples of Valid Binary Integers
Examples of Invalid Binary Integers