Learn C++
#include "variablesdatatypes/literals.hpp"

Literals in C++

//concept

A literal is a value written directly in source: 42, 3.14, 'A', "hello", true. C++ defaults integer literals to int and decimal ones to double, with suffixes to override — U for unsigned, L/LL for long/long long, f/F for float — and prefixes for bases: 0x for hex, 0b for binary, 0 for octal.

Digit separators (single quotes inside numbers, like 1'000'000) and user-defined literal suffixes round out the system. The single most practical rule: floating literals assigned to float need the f suffix, or the double-to-float conversion warns.

//how it works
1

Integer literals and suffixes

42 is int; 42U is unsigned, 42L is long, 42LL is long long — suffixes pick the type when the default doesn't fit.

2

Floating literals default to double

3.14 is double; 3.14f is float. Assigning a bare decimal to a float variable narrows and may warn.

3

Characters, strings, booleans

'A' is a char, "A" is a string (array of chars plus terminator) — the quote style changes the type, not just the look.

4

Bases and separators

0xFF (hex), 0b1010 (binary), 0755 (octal) write the same ints in other bases; 1'000'000 uses quotes as digit separators for readability.

//example
#include <iostream>
 
int main() {
int hex = 0x2A;
long long big = 3000000000LL;
float f = 2.5f;
std::cout << hex << ", " << big << ", " << f << std::endl;
return 0;
}
Output
42, 3000000000, 2.5
10x2A is hexadecimal for 42 — the compiler stores a plain int, which is what prints.
23 billion exceeds int range, so the LL suffix makes it long long; without it the literal would be ill-formed.
32.5f is genuinely float, matching f's type with no narrowing conversion involved.
//use cases
Writing bit masks and flags as 0xFF-style hex instead of inscrutable decimals
Adding LL to large constants (timestamps, byte counts) that overflow int
Remembering the f suffix whenever a literal initializes a float
Using digit separators in long constants so 1000000000 reads as 1'000'000'000

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.