Literals in C++

What are Literals ?

At its core, a literal is a fixed value that appears directly in the source code of a program. When the program is compiled, these values remain constant and cannot be changed during the execution of the program. C++ supports various types of literals, and each type serves a specific purpose.
Literals in c++

Introduction

In the world of C++, literals play a crucial role in programming. They are constants used to represent fixed values in code. These values can be of various types, including integers, floating-point numbers, characters, and strings. Understanding literals is fundamental to writing efficient and concise C++ code. In this page, we will dive deep into the concept of literals and explore their various types and usage.

Consider the following examples:

Literals Example
Integer Literal int myInteger = 42;
Floating-Point Literal float myFloat = 3.14;
Character Literal char myChar = ‘A’;
String Literal std::string myString = “Hello, world!”;
Boolean Literal stbool isTrue = true;
Null Pointer Literal int* myPointer = nullptr;

Integer Literals

Integer literals represent whole numbers without a fractional or decimal component. They can be specified in different formats, such as decimal, octal, or hexadecimal. C++ also allows the use of binary literals for better readability in representing binary numbers.

Literals Example
Decimal int myDecimal = 42
Octal int myoctal = 052
Hexadecimal int myHex = ox2A
Binary int myBinary = 0b101010

Floating-Point Literals

Floating-point literals are used to represent real numbers with fractional parts. They consist of a whole number part, a decimal point, a fractional part, and an optional exponent. Programmers can use either the traditional notation or scientific notation to represent floating-point literals.

Literals Example
Traditional Notation double myDouble = 3.14;
Scientific Notation: double mySciNotation = 3.0e8;

Character Literals

Character literals represent single characters and are enclosed within single quotes (”). They allow programmers to work with individual characters in C++.

Literals Example
Character Literal char myChar = ‘A’;

String Literals

String literals, on the other hand, represent sequences of characters and are enclosed within double quotes (“”). They allow programmers to handle textual data, making string manipulation much more manageable.

Literals Example
String Literal std::string myString = “Hello, world!”;

Boolean Literals

Boolean literals represent truth values and can take one of two values: true or false. They are essential in conditional statements and logical operations.

Literals Example
Boolean Literal bool isTrue = true;

Null Pointer Literal

C++ introduces the concept of the null pointer literal, represented by the keyword “nullptr.” It is used to indicate that a pointer is not pointing to any memory location.

Literals Example
Null Pointer Literal int* myPointer = nullptr;

User-Defined Literals

One of the exciting features of C++ is user-defined literals. Programmers can define their own custom literals to work with specific data types and provide a more expressive syntax for their applications.

constexpr long double operator"" _inch(long double x) {
    return x * 2.54;
}

// Usage:
long double lengthInCm = 10.0_inch; // Converts 10 inches to centimeters

Literal Suffixes

C++ allows literal suffixes to be appended to numeric literals. These suffixes indicate the data type of the literal, providing control over the type and size of the literal.

long long myLong = 42LL;
float myFloat = 3.14f;
unsigned int myUnsigned = 42u;

Combining Literals with Variables and Constants

Literals are often used in conjunction with variables and constants in C++. Understanding how to combine literals with other elements of the language is essential for writing powerful and flexible programs.
int radius = 5;
double area = 3.14 * radius * radius;

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription