• Home
  • 1.14 Integer Constants and Bases

1.14 Integer Constants and Bases

View Categories

1.14 Integer Constants and Bases

< 1 min read

Integer constants can be written using different number bases. C supports decimal, octal, and hexadecimal integer constants; binary integer constants are supported in C23.

#include <stdio.h>

int main()
{
    int decimal = 25;
    int octal = 031;
    int hexadecimal = 0x19;

    printf("Decimal      = %d\n", decimal);
    printf("Octal        = %d\n", octal);
    printf("Hexadecimal  = %d\n", hexadecimal);

    return 0;
}

Example Output #

Decimal      = 25
Octal        = 25
Hexadecimal  = 25

Explanation #

A decimal integer constant uses the digits 0 through 9 without a base prefix:

25

An octal integer constant begins with 0:

031

031 represents decimal 25.

A hexadecimal integer constant begins with 0x or 0X:

0x19

0x19 also represents decimal 25.

Integer Constant Prefixes #

Notation Base Example Decimal value
No prefix 10 25 25
0 8 031 25
0x / 0X 16 0x19 25
0b / 0B 2 0b11001 25

0b and 0B binary integer constants are standardized in C23. Earlier C standards do not define them as standard integer-constant syntax, although some compilers provide them as extensions.

Hexadecimal Digits #

Hexadecimal uses sixteen digits:

Decimal Hexadecimal
0–9 0–9
10 A / a
11 B / b
12 C / c
13 D / d
14 E / e
15 F / f

For example:

int value = 0x2A;

0x2A represents decimal 42.

Integer constants can also use suffixes such as U, L, and LL to control their type.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *