Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Literals

Reference for C++ literals: integer, floating, character and string literals, prefixes and suffixes, digit separators, and user-defined literals.

A literal is a value written directly in the source code: 42, 3.14, 'x', "hello", true. Every literal has a type, and the way you spell it (prefix, suffix, quote style) decides which type. Knowing the rules prevents surprises such as 1/2 being 0, 'a' + 1 being an int, or a string literal being a const char[N] instead of a std::string.

C++ also lets you define your own suffixes, called user-defined literals, so that 5_km, "text"s or 250ms create objects of a library or your own type.

C++
auto a = 255;          // int
auto b = 0xFFu;        // unsigned int
auto c = 1.5f;         // float
auto d = "hi"s;        // std::string (with using namespace std::string_literals)
auto e = 90_deg;       // your own type, via operator""_deg

Try it

C++ Integer, floating and character literals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ String literals and user-defined literals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

Kind Examples Type
Decimal / octal / hex / binary integer 42, 052, 0x2A, 0b101010 int, or the first of long, long long that fits (decimal); for other bases unsigned types are also tried.
Integer suffixes u, l, ul, ll, ull, z, uz (C++23) unsigned, long, unsigned long, long long, unsigned long long, the signed counterpart of std::size_t, std::size_t
Digit separator 1'000'000, 0xFF'EC Ignored; groups digits (C++14).
Floating 1.5, .5, 2., 1e-3, 0x1p-2 double; suffix f is float, l is long double.
Character 'a', '\n', '\x41', u8'a', u'a', U'a', L'a' char, char8_t, char16_t, char32_t, wchar_t.
String "text", u8"text", u"text", U"text", L"text" const char[N] etc., N includes the terminating zero.
Boolean / pointer true, false, nullptr bool, std::nullptr_t.
User-defined 42_km, "s"_x, 1.5_deg Whatever operator""_suffix returns.

Defining a literal operator: R operator""_name(unsigned long long) for integers, (long double) for floating, (char)/(const char*, std::size_t) for chars and strings, or (const char*) for the raw token. Suffixes without a leading underscore are reserved for the standard library.

Errors / undefined behavior: an integer literal too large for its type is ill-formed (a compile error or warning). Modifying a string literal is undefined behavior, which is why its type is const char[N].

Usage notes

  • Pick the suffix to avoid overflow: 1 << 40 overflows int; 1ULL << 40 does not. 1'000'000 * 1'000'000 also overflows int.
  • 0 prefix means octal. 010 is 8. Never pad numbers with leading zeros.
  • 'ab' (multi-character literal) is allowed but has an implementation-defined value; avoid it.
  • String literals are arrays, so sizeof("hi") is 3. Adjacent literals concatenate.
  • Use s and sv suffixes for std::string and std::string_view; they need using namespace std::literals (or std::string_literals).
  • Chrono literals: 1s, 250ms, 3h, 2d (C++20), 2020y (C++20). They need using namespace std::chrono_literals;.
  • User-defined literals are only syntax sugar for calling a function. Keep them to cases where units or types make code more readable.
  • Raw strings have their own page: Raw string literals.