Literals in C++

Last Updated : 16 Jan, 2026

In C++ programming language, literals are fundamental elements used to represent fixed values. These values can include numbers, characters, strings, and more. They are generally present as the right operand in the assignment operation.

For example:

C++
#include <iostream>
using namespace std;

int main() {
  	// Assigning an integer value to integer variable
    int val = 22;
  	
  	cout << val << endl;
  	cout << 22 << endl;
  
  	// Assigning a new value
  	val = 6;
  	cout << val << endl;
  	cout << 22;
    return 0;
}

Output
22
22
6
22

Explanation: In the above example, the val variable is first assigned a value 22. This value is a literal of integer type. Now further in the program, the value of val may change, but the integer literal 22 will always represent the natural number 22 no matter where it is used.

C++ supports various types of literals to represent the values of different data types. In this article, we will discuss all the necessary information on C++ literals and their usage.

c_literals
Types of literals in C++

Types of Literals in C++

There are five types of literals in C++ based on the type of information they represent:

1. Integer Literals

Integer literals in C++ are values that represent whole numbers without the fractional or decimal part. They can be positive or negative and can have different bases such as decimal, octal, or hexadecimal. In C++, integer literals can be categorized into various types based on their bases.

1. Decimal Literal (No Prefix): The decimal literal represents an integer in base 10 without any prefix.

C++
#include <iostream>
using namespace std;

int main() {
    int dec= 42;
    cout << dec << endl;
    return 0;
}

Output
42

2. Octal Literal (Prefix: 0): The octal literal represents an integer in base 8 and is preceded by the 0 prefix.

C++
#include <iostream>
using namespace std;

int main() {
    int oct= 052;
    cout << oct << endl;
    return 0;
}

Output
42

3. Hexadecimal Literal (Prefix: 0x or 0X): The hexadecimal literal represents an integer in the base 16 and is preceded by either 0x or 0X prefix.

C++
#include <iostream>
using namespace std;

int main() {
    int hex= 0x2A;
    cout << hex << endl;
    return 0;
}

Output
42

4. Binary Literal (Prefix: 0b or 0B): The binary literal represents an integer in the base 2 and can have a suffix of 0b or 0B.

C++
#include <iostream>
using namespace std;

int main() {
    int bin= 0b101010;
    cout << bin << endl;
    return 0;
}

Output
42

5. Long Integer Literal (Suffix: l or L): The long integer literal can be suffixed with the l or L to indicate that it is of type long int.

C++
#include <iostream>
using namespace std;

int main() {
    long int lint= 42L;
    cout << lint << endl;
    return 0;
}

Output
42

6. Unsigned Integer Literal (Suffix: u or U): The unsigned integer literal can be suffixed with the u or U to indicate that it is of type unsigned int.

C++
#include <iostream>
using namespace std;

int main() {
    unsigned int uint= 42U;
    cout << uint << endl;
    return 0;
}

Output
42

7. Long Long Integer Literal (Suffix: ll or LL): The long long integer literal can be suffixed with the ll or LL to indicate that it is of type long long int.

C++
#include <iostream>
using namespace std;

int main() {
    long long int llint= 42LL;
    cout << llint << endl;
    return 0;
}

Output
42

2. Floating-Point Literals

The Floating-point literals in C++ are the values that represent numbers with the fractional or decimal part. These can be either single-precision or double-precision values.

Like integer literals, the floating-point literals can also be defined as double or float by using suffix d and f respectively. By default, all the fractional numbers are considered to be of type double by C++ compiler if not specified.

1. Float Literals (Suffix: f or F): Floating point literals are specified by adding f or F as a suffix to fractional values.

C++
#include <iostream>
using namespace std;

int main() {
    float f = 3.14f;
    cout << f << endl;
    return 0;
}

Output
3.14

2. Double Literals (Suffix: d or D): The double precision floating point literals are the default literals for fractional values but can also be manually specified.

C++
#include <iostream>
using namespace std;

int main() {
    double d = 3.14;
    cout << d << endl;
    return 0;
}

Output
3.14

3. Long Double Literals (Suffix: l or L): The long double floating point literals can be specified using l or L as a suffix to fractional values.

C++
#include <iostream>
using namespace std;

int main() {
    long double ld = 3.14L;
    cout << ld << endl;
    return 0;
}

Output
3.14

Note: If there is no decimal point, that literal will be treated as long int.

4. Scientific Notation: The scientific notations help us to represent very large or small value in compact form.

C++
#include <iostream>
using namespace std;

int main() {
    long double sld = 1.22e11;
    cout << sld << endl;
    return 0;
}

Output
1.22e+11

3. Character Literals

The character literals in C++ are values that represent individual characters or escape sequences. They are enclosed in single quotes, and you can use them to represent the single character. As C++ using ASCII character set, there are 128 possible character literals.

For example:

C++
#include <iostream>
using namespace std;

int main() {
  	// Character variable assigned to character literal
    char c = 'A';
    cout << c;
    return 0;
}

Output
A

4. String Literals

The string literals in C++ are the sequences of the characters enclosed in double quotes, and you can use them to represent text or character sequences. A string literal doesn't need to be a meaningful word, it can be any random collection of one or more characters.

For example:

C++
#include <iostream>
using namespace std;

int main() {
  	// String assigned to a string literal
    const char* s = "Hello, GeeksforGeeks!";
    cout << s;
    return 0;
}

Output
Hello, GeeksforGeeks!

Note: The string literals are the only literals that are not rvalues.

5. Boolean Literals

The boolean literals represent the truth values and have only two possible values: true and false. These values are used in the Boolean expressions and logic operations.

For example:

C++
#include <iostream>
using namespace std;

int main() {
    bool isTrue = true;
    bool isFalse = false;
  
    if (isTrue)
        cout << "isTrue is true" << endl;
    else
        cout << "isTrue is false" << endl;

    if (isFalse)
        cout << "isFalse is true" << endl;
    else
        cout << "isFalse is false" << endl;
  
    return 0;
}

Output
isTrue is true
isFalse is false
Comment