Integer Literals

Last modified by Microchip on 2023/11/09 09:06

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

05127-102165535

Examples of Invalid Decimal Integers

32,76725.01 02405527A

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

0x0x10x0A2B0x120xBEEF

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

0x5.30EA120xEG53h

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

 01012073125

Examples of Invalid Octal Integers

05.30o1208053o

Binary (base 2)

Warning

Caution: 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

0b0b10b0101001100001111

Examples of Invalid Binary Integers

0b1.0011000b1210b