1. Intro
A literal is a fixed value written directly into the source code — it represents itself exactly, with no name attached. In int age = 20;, the number 20 is a literal: it is not a variable, it is the raw value itself.
Cricket analogy: When a scorer writes '20' as a batsman's score, that number isn't a variable label — it's the literal, exact runs scored, just as 20 in int age = 20; is the raw literal value itself.
2. Syntax
C++ supports five main categories of literals:
Cricket analogy: Just as a cricket scorecard tracks five distinct stat types — runs, wickets, overs, extras, and strike rate — C++ organizes literals into five distinct categories, each with its own rules.
- Integer literals — 10, -5, 0x1A (hexadecimal), 012 (octal), 0b101 (binary)
- Floating-point literals — 3.14, 2.5f, 1.6e3 (scientific notation)
- Character literals — 'A', '9', '\n'
- String literals — "Hello, World!"
- Boolean literals — true, false
3. Explanation
Integer literals can be written in decimal (base 10) by default, but also in octal (prefix 0), hexadecimal (prefix 0x), or binary (prefix 0b, C++14 and later). A suffix can specify the exact type: U for unsigned, L for long, and LL for long long — for example, 100UL is an unsigned long literal.
Cricket analogy: A team total can be reported as '250 all out' (decimal) or broken down differently for a Duckworth-Lewis revision, just as an integer literal can be written in decimal, octal (0-prefix), hex (0x), or binary (0b), with a suffix like 100UL specifying it as unsigned long.
Floating-point literals are double by default. Appending f or F makes a literal a float instead (e.g. 3.14f), and appending l or L makes it a long double. Character literals are always written in single quotes and hold exactly one character, while string literals use double quotes and can hold a sequence of characters (internally, a C-style string literal is null-terminated automatically).
Cricket analogy: A batting average like 45.67 is treated as a precise double by default, but a quick estimate 45.5f rounds to a lighter float, just as C++ floating literals default to double unless suffixed f; a player's grade 'A' is a char, while their full name "Kohli" is a null-terminated string.
Pro Tip
without an f suffix, 3.14 is treated as a double. Assigning it directly to a float (float x = 3.14;) still works via implicit conversion, but writing 3.14f is more explicit and avoids an unnecessary double-to-float narrowing conversion.
4. Example
#include <iostream>
using namespace std;
int main() {
int decimalVal = 42;
int hexVal = 0x2A; // same as 42 in hexadecimal
float priceF = 9.99f;
double preciseVal = 3.14159;
char grade = 'A';
string greeting = "Hello, C++!";
bool isReady = true;
cout << "Decimal: " << decimalVal << endl;
cout << "Hex value: " << hexVal << endl;
cout << "Price: " << priceF << endl;
cout << "Precise: " << preciseVal << endl;
cout << "Grade: " << grade << endl;
cout << "Greeting: " << greeting << endl;
cout << "Ready: " << isReady << endl;
return 0;
}5. Output
Decimal: 42
Hex value: 42
Price: 9.99
Precise: 3.14159
Grade: A
Greeting: Hello, C++!
Ready: 16. Key Takeaways
- A literal is a fixed value written directly in code, with no identifier attached.
- C++ has integer, floating-point, character, string, and boolean literals.
- Integer literals can be written in decimal, octal, hexadecimal, or binary form.
- Suffixes like f, L, and U specify the exact type of a numeric literal.
Practice what you learned
1. What is a literal in C++?
2. Which prefix denotes a hexadecimal integer literal?
3. What data type is a floating-point literal like `3.14` by default?
4. How do you write a float literal instead of a double literal for the value 9.99?
5. Which of these is a valid character literal?
Was this page helpful?
You May Also Like
Constants in C++
Learn what constants are in C++, the different ways to define them (const, #define, constexpr), and when to use each.
Data Types in C++
Learn the fundamental data types in C++ — int, float, double, char, bool, and void — with sizes, ranges, and examples.
Escape Sequences in C++
Learn what escape sequences are in C++, the full list of common ones, and how to use them inside strings and characters.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics