C int type literal

Decimal int literal

An integer constant can be expressed as an ordinary decimal numeral, or as a numeral in octal or hexadecimal notation.

A decimal constant begins with a nonzero digit. For example, 255 is the decimal constant for the base-10 value 255.

Octal int literal

A number that begins with a leading zero is interpreted as an octal constant. Octal (or base eight) notation uses only the digits from 0 to 7. For example, 047 is a valid octal constant representing 4 x 8 + 7, and is equivalent with the decimal constant 39.

The decimal constant 255 is equal to the octal constant 0377.

Hexadecimal int literal

A hexadecimal constant begins with the prefix 0x or 0X. The hexadecimal digits A to F can be upper- or lowercase. For example, 0xff, 0Xff, 0xFF, and 0XFF represent the same hexadecimal constant, which is equivalent to the decimal constant 255.

Suffix example

Suffixes are the signs that we use to mark the literal type. Examples of constants with suffixes are listed in the following table.

Integer constant Type
0x200 int
512U unsigned int
0L long
0Xf0fUL unsigned long
0777LL long long
0xAAAllu unsigned long long

Example for Octal numbers


#include <stdio.h>
main()//from ww  w. j av a2 s.c  om
{
    int i = 065;
   
    printf("%d", i);

    i = 65;
    printf("%o", i);
}

Represent a number by using the octal number system. The octal number system that is, base 8. Use "%o" to print octal numbers.

Example - Hexadecimal numbers

Hexadecimal numbers use base 16. The characters used in hexadecimal numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

You can print numbers in hexadecimal form by using the format "x".


#include <stdio.h>
//ww w  .j a  v a 2s . c o  m
main()
{
    int i = 65;
   
    printf("%x", i);
}




















Home »
  C Language »
    Language Basics »




C Language Basics
Data Types
Operator
Statement
Array