Skip to main content
Engineering LibreTexts

4.4.1: Literals and Constants - Integers

  • Page ID
    29032
  • Integer Literals

     These are used to represent and store the integer values. Integer literals are expressed in two types i.e.,

    1. Prefixes: The Prefix of the integer literal indicates the base in which it is to be read.
      For example:
      0x10 = 16
      
      Because 0x prefix represents a HexaDecimal base.
      So 10 in HexaDecimal is 16 in Decimal.
      Hence the value 16. 
      

      There are basically represent in four types.

      1. Decimal-literal(base 10): A non-zero decimal digit followed by zero or more decimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

        For example:

        56, 78
        
      2. Octal-literal(base 8): a 0 followed by zero or more octal digits(0, 1, 2, 3, 4, 5, 6, 7).

        For example:

        045, 076, 06210
        
      3. Hex-literal(base 16): 0x or 0X followed by one or more hexadecimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F).

        For example:

        0x23A, 0Xb4C, 0xFEA
        
      4. Binary-literal(base 2): 0b or 0B followed by one or more binary digits(0, 1).

        For example:

        0b101, 0B111
        
    2. Suffixes: The Prefix of the integer literal indicates the type in which it is to be read.

      For example:

      12345678901234LL 
      indicates a long long integer value 12345678901234
      because of the suffix LL
      

      These are represented in many ways according to their data types.

      1. int: No suffix is required because integer constant is by default assigned as an int data type.
      2. unsigned int: character u or U at the end of an integer constant.
      3. long int: character l or L at the end of an integer constant.
      4. unsigned long int: character ul or UL at the end of an integer constant.
      5. long long int: character ll or LL at the end of an integer constant.
      6. unsigned long long int: character ull or ULL at the end of integer constant.

    Example:

    #include <iostream> 
    using namespace std; 
    
    int main() 
    {   // constant integer literal 
    
        const int intVal = 10; 
    
        cout << "Integer Literal: " << intVal << "\n"; 
        return 0; 
    } 
    
    Integer Literal: 10
    

    Adapted from:
    "Types of Literals in C/C++ with Examples" by Chinmoy Lenka, Geeks for Geeks