Tutorial Study Image

C++ Literals and Constants


July 21, 2022, Learn eTutorial
1241

Literals are another name for constants, which are fixed values that a program is not permitted to change. Constants can be any of the fundamental data types and are further broken down into Boolean Values, Characters, Strings, Floating-Point Numbers, and Integer Numericals. Constants are treated the same as regular variables, but they cannot have their values changed once they have been defined.

Data used to represent fixed values are called literals. They can be inserted right into the code. For eg, 9, 11.5, "b," etc.

9, 11.5, and "b" are literals in the above example. This is because these terms won't be able to assign values that are different.

How we will define a constant?

Constants can be defined in C/C++ programs in one of two ways, as demonstrated below:

  • Using #define preprocessor directive
  • Using a const keyword
  1. Using #define preprocessor directive: This directive declares an alias name for an existing variable or any value. As illustrated below, we may use this to declare a constant.
    
    #define identifierName value
    
    
    • identifierName: It is the name that is given to a constant.
    • Value : This refers to any value that has been assigned to identifierName.
    How we will define a constant?
  2. Using a const keyword: Defining constants with the const keyword is the same as defining variables; the only difference is that the definition must be preceded by the const keyword using the term const.
    
    const datatype variable_name = value
    
    
    How we will define a constant?

Different types of literal used in C++ programming

1. Integer literals

A decimal, octal, or hexadecimal constant can be an integer literal. The base or radix is specified by a prefix, which can be 0 for hexadecimal, 0 for octal, and for decimal, it will be nothing. Another possible suffix for an integer literal is a combination of the letters U and L, which stand for unsigned and long. The suffix can be in any order and in either capital or lowercase letters. A numeric literal (one connected with numbers) without a fractional or exponential component is called an integer.

Mainly there are 3 types of integer literals in c programming and they are :(link to c tutorial)

  • Decimal
  • Octal
  • Hexadecimal

In the case of C++ programming octal always starts with 0 and the hexadecimal will always start with 0x

Integer literals in detail with simple examples.

The integer values are represented and stored using integer literals.   There are two ways to express integer literals, i.e. 

  1. Prefixes: The base in which the integer literal is to be read is indicated by its prefix. For example :  0 x 20=32

    The HexaDecimal base is represented by the prefix 0x. So 20 in HexaDecimal is 32 in Decimal. Hence the value will be 32.

    Mainly there are 4 types of integer literals in c++ programming and they are :

  2. Suffixes: The type in which the integer literal is to be read is indicated by its suffix. That means it mainly indicates the type.

    For example, 123496098764321LL  mainly indicates that the value 12345678901234 is a long long integer with the suffix LL.

    Depending on the different data kinds, these are represented in various ways.

    • int: Since integer constants are assigned as int data types by default, there is no need for a suffix.
    • unsigned int: Character u or U will be mainly seen at the end of an integer constant.
    • long int: Character l or L  will be mainly seen at the end of an integer constant.
    • unsigned long int: Character ul or UL  will be mainly seen at the end of an integer constant.
    • long long int: Character ll or LL  will be mainly seen at the end of an integer constant.
    • unsigned long long int: Character ull or ULL  will be mainly seen at the end of an integer constant.

How are the integer literals used in the C++ program?

How are the integer literals used in the C++ program?


#include<iostream> 
int main()
{
 
    // constant integer literal is used here
    
    const int intVal = 30;
 
    printf("Integer Literal Will Be:%d \n",
            intVal);
    return 0;
}

Output:


Integer Literal Will Be : 30

Floating point literals

Real numbers are represented and stored using floating point literals. Real numbers are composed of integer, real, fractional, and exponential components. Either decimal or exponential form can be used to hold floating-point literals. 

Two considerations must be made when representing floating-point decimals in order to construct valid literals:

  • The decimal point, exponent portion, or both must be included in the decimal form in order to avoid errors.
  • The integer component, fractional part, or both must be included in the exponential form; otherwise, an error will occur.

Valid Floating literals and invalid floating literals are a few floating-point literals representations.

Example For Valid Floating literals Example for Invalid Floating Literals
10.541
1.123-20L
10.6E-2
126E
1230f
0.e678

How the floating-point literals are used in the C++ program?

How the floating-point literals are used in the C++ program?


#include <iostream>
using namespace std;
 
int main()
{
    // Real literal
    const float floatVal = 3.14;
 
    cout << "Floating-point literal Will be: "
         << floatVal << "\n";
    return 0;
}

Output:


Floating-point literal Will be:3.14

Boolean literals

This literal is only offered in C++; it is not available in C. The boolean data types are represented by them.

These have two possible values:

  • true: true is mainly used in order to represent True Value. This should not be considered equal to int 1
  • false: Used to indicate a false value. This should not be considered equal to int 0

How are the Boolean literals used in the C++ program?

How the boolean literals are used in the C++ program?


#include <iostream>
using namespace std;
 
int main()
{
    const   bool isTrue = true;
    const   bool isFalse = false;
 
    cout << "isTrue then it is? "
        << isTrue << "\n" ;
    cout << "isFalse then it is? "
        << isFalse << "\n" ;
 
    return 0;
}

Output:


isTrue then it is? 1
isFalse then it is? 0

Character literals

The literal that is used to store a single character inside of a single quote is the subject of this sentence. A character array must be used to store multiple characters. A warning will be issued and just the last character of the literal will be displayed if more than one character is stored within a single quote.

The two representations that result from it are as follows:

  • char type: The narrow-character literals or normal character literals are stored here.

    For example : // For C++

    char chr = 'R';

  • wchar_t type: Only C++ supports this literal; C does not. The literal must be stored in wchar t if the character is followed by an L. 

    for example : // For C++

    wchar_t chr = L'R';

  • Escape sequence: In order to perform various operations there are many special characters.

How the Character literals are used in the C++ program?

How are the Character literals used in the C++ program?


#include 
 
int main()
{
    // constant char literal
    const char charVal = 'A';
 
    printf("Character Literal Will Be: %c\n",
        charVal);
    return 0;
}


Output:


Character Literal Will Be : A

String literals

Character literals and string literals are similar. Except that they can store numerous characters and they mainly uses a double quote inorder to store the same.

For Example: 

string stringVal = "Learn eTutorials"
 

How the string literals are used in C++ program?


#include 
using namespace std;
 
int main()
{
    const string str
        = "Welcome All\nTo\n Learn\t e \t Tutorials";
    cout << str;
    return 0;
}

}

Output:


Welcome All
To
Learn e Tutorials