Integer Constants in C Programming

Integer Constants

Integer constants are whole numbers without any fractional part. Thus integer constants consist of a sequence of digits. Integer constants can be written in three different number systems: Decimal, Octal and Hexadecimal.

  • Decimal: Base 10 numbers like 0, 1, 2, 3, ...
  • Octal: Base 8 numbers represented with a leading '0' like 017, 020, ...
  • Hexadecimal: Base 16 numbers prefixed with '0x' or '0X' like 0x1F, 0xA3, ...

A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit must be something other than 0. The following are valid decimal integer constants.

0 1 1234 -786

Example:

The following decimal integer constants are written incorrectly for the reasons stated.

12,001 Illegal character (,).
103.0 Illegal decimal point (.).
10 20 30 Illegal character (blank space).
045 First digit cannot be zero.

Example:

An octal integer constant can consist any combination of digits taken from the set 0 through 7. However, the first digit must be 0, in order to identify the constant as an octal number. The following are valid octal integer constants.

0 01 0125 0555

Example :

The following decimal integer constants are written incorrectly for the reasons stated.

2010 Does not begin with 0.
2008 Illegal digit (8).
05.10 Illegal character (.).

A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and A through F (either upper-case or lower-case). The following are valid hexadecimal integer constants.

0X0 0x1 0XAB125 -0x555

Example:

The following hexadecimal integer constants are written incorrectly for the reasons stated.

0x20.10 Illegal character (.).
02008 Does not begins with 0x or 0X.
0xabcdefg Illegal character (g).

Other basic types of constants in C are:

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!