Literals in C++
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.
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.
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.
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.
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.
#include <iostream> int main() { int hex = 0x2A; long long big = 3000000000LL; float f = 2.5f; std::cout << hex << ", " << big << ", " << f << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.