Literals in C++

In C++, a literal is a fixed value that is written directly into the source code. Unlike variables, whose values can change during program execution, literals always represent the same value. Whenever you write a number, a character, or a piece of text directly in your program, you are using a literal.

For example, in the statement int age = 20;, the value 20 is a literal because it is written directly in the code. Similarly, "Hello" and 'A' are also literals.

Understanding literals is important because they are used in almost every C++ program, whether you are storing values in variables, performing calculations, or displaying output.

Types of Literals in C++

C++ provides several types of literals. Each type represents a different kind of value.

Integer Literals

Integer literals are whole numbers without a decimal point. They can be positive or negative.

Example

int age = 25;
int marks = 90;

Here, 25 and 90 are integer literals.

Floating-Point Literals

Floating-point literals represent decimal numbers.

Example

float price = 99.99;
double pi = 3.14159;

In this example, 99.99 and 3.14159 are floating-point literals.

Character Literals

Character literals store a single character enclosed in single quotes.

Example

char grade = 'A';
char symbol = '#';

Both 'A' and '#' are character literals.

String Literals

String literals are a sequence of characters enclosed in double quotes.

Example

string name = "CoderMantra";
string city = "Delhi";

The values "CoderMantra" and "Delhi" are string literals.

Boolean Literals

Boolean literals represent logical values. They have only two possible values.

Example

bool isPassed = true;
bool isLoggedIn = false;

Here, true and false are boolean literals.

C++ Literals with Examples

The following program uses different types of literals together.

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    float height = 5.9;
    char grade = 'A';
    string name = "Rahul";
    bool passed = true;

    cout << name << endl;
    cout << age << endl;
    cout << height << endl;
    cout << grade << endl;
    cout << passed;

    return 0;
}

In this example, every value assigned directly to a variable is a literal. These values help initialize variables before they are used in the program.

Key Points

  • Literals in C++ are fixed values written directly in the source code.
  • Integer, floating-point, character, string, and boolean are the most commonly used literal types.
  • Literals are used to assign values to variables.
  • Character literals use single quotes, while string literals use double quotes.
  • Choosing the correct literal type makes your programs easier to read and understand.

Frequently Asked Questions

Q1. What are literals in C++?

Literals in C++ are fixed values written directly in a program. They represent constant values such as numbers, characters, strings, and boolean values.

Q2. What are the different types of literals in C++?

The main types of literals in C++ are integer literals, floating-point literals, character literals, string literals, and boolean literals.

Q3. What is the difference between literals and variables in C++?

A literal is a fixed value written directly in the code, whereas a variable is a named memory location used to store data that can change during program execution.

Q4. Can literals be modified in C++?

No. Literals are constant values and cannot be changed while the program is running. To store a value that may change, you should use a variable.

Q5. What is a string literal in C++?

A string literal is a sequence of characters enclosed in double quotation marks, such as "Hello, World!". It is commonly used to store and display text.

Q6. Why are literals important in C++?

Literals provide fixed values that are used in expressions, assignments, calculations, and function calls. They form one of the fundamental building blocks of every C++ program and help represent constant data directly in the code.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top