100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Literals in C++

Learn what literals are in C++ — integer, floating-point, character, string, and boolean literals — with syntax and examples.

BasicsBeginner6 min readJul 7, 2026
Analogies

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

cpp
#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

text
Decimal: 42
Hex value: 42
Price: 9.99
Precise: 3.14159
Grade: A
Greeting: Hello, C++!
Ready: 1

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#LiteralsInC#Literals#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep