Ch2.3: Literals
What Are Literals?
A literal is a fixed value written directly in your code. Unlike variables, literals do not change — they are constants embedded in the program.
Examples
#include <fast_io.h>
#include <cstddef>
#include <cstdint>
#include <stdfloat>
int main()
{
using namespace ::fast_io::iomnp;
// Integer literal assigned to ::std::size_t
::std::size_t number = 42;
println(number); // prints 42
// Floating point literal assigned to ::std::float64_t
::std::float64_t pi = 3.14;
println(pi); // prints 3.14
// Character literal
println('A'); // prints 65 (ASCII code)
println(chvw('A')); // prints A
// Hexadecimal escape sequence gives value directly
println('\x41'); // prints 65
println(chvw('\x41')); // prints A
// Assigning character literal 'A' to different integer types
char c = 'A';
::std::size_t b = 'A';
::std::uint_least16_t u16 = 'A';
::std::int_least32_t i32 = 'A';
println(c); // prints 65
println(b); // prints 65
println(u16); // prints 65
println(i32); // prints 65
println(chvw(c)); // prints A
// UTF-specific character types
char8_t u8ch = u8'A';
char16_t u16ch = u'A';
char32_t u32ch = U'A';
println(u8ch); // prints 65
println(u16ch); // prints 65
println(u32ch); // prints 65
println(chvw(u8ch)); // prints A
println(chvw(u16ch));// prints A
println(chvw(u32ch));// prints A
// Boolean literals
println(true, " ", false); // prints 1 0
println(boolalpha(true), " ", boolalpha(false)); // prints true false
// String literals (adjacent concatenation)
print("Hello"
"World"
"from C++\n");
// Multiline string literals
print("Hello\n"
"World\n"
"from C++\n");
// UTF string literals must use code_cvt
println(code_cvt(u8"Hello UTF-8"));
println(code_cvt(u"Hello UTF-16"));
println(code_cvt(U"Hello UTF-32"));
}
Escape Sequences
Escape sequences let you represent special characters inside string and character literals.
They start with a backslash \.
| Escape sequence | Meaning | Example |
|---|---|---|
\n | New line | "Hello\nWorld" |
\t | Horizontal tab | "A\tB\tC" |
\r | Carriage return | "Line1\rLine2" |
\v | Vertical tab | "Hello\vWorld" |
\\ | Backslash | "Backslash: \\" |
\" | Double quote | "Quote: \"Hello\"" |
\' | Single quote | "It\'s fine" |
\x41 | Hexadecimal value 0x41 | '\x41' → A |
Raw String Literals
Raw string literals let you write text without escaping backslashes or quotes.
They start with R"( ... )" and can span multiple lines.
UTF prefixes (u8R, uR, UR) are also supported.
#include <fast_io.h>
int main()
{
using namespace ::fast_io::iomnp;
// Multiline raw string literal
println(R"(Line1
Line2
Line3)");
// Raw string with quotes and backslashes
println(R"(Path: C:\Users\Name
Quote: "Hello World")");
// UTF raw string literals
println(code_cvt(u8R"(Hello UTF-8 raw)"));
println(code_cvt(uR"(Hello UTF-16 raw)"));
println(code_cvt(UR"(Hello UTF-32 raw)"));
// Raw string with custom delimiter
println(R"abc(This is a raw string with a custom delimiter)abc");
}
Multiline String Literals for Other Char Types
UTF string literals (u8"...", u"...", U"...") can also span multiple lines.
Adjacent literals are concatenated at compile time.
When printing with fast_io, wrap them in code_cvt(...) because the default device expects char.
#include <fast_io.h>
int main()
{
using namespace ::fast_io::iomnp;
println(code_cvt(u8"Hello\n"
u8"World\n"
u8"from UTF-8"));
println(code_cvt(u"Hello\n"
u"World\n"
u"from UTF-16"));
println(code_cvt(U"Hello\n"
U"World\n"
U"from UTF-32"));
}
print vs println
In fast_io, both print and println write output to the screen.
The difference is simple:
print(...)— prints exactly what you give it.println(...)— prints what you give it plus an extra"\n"at the end.
Binary, Hexadecimal, and Octal Literals
In addition to decimal numbers, C++ allows you to write integer literals in binary, hexadecimal, and octal form. These are useful when working with bit patterns, memory addresses, or low-level data.
0b...→ Binary literal (base 2)0x...→ Hexadecimal literal (base 16)0...→ Octal literal (base 8)
#include <fast_io.h>
#include <cstdint>
int main()
{
using namespace ::fast_io::iomnp;
// Binary literal
::std::uint_least32_t bin = 0b1010; // decimal 10
println("Binary 0b1010 = ", bin);
// Hexadecimal literal
::std::uint_least32_t hex = 0x1F; // decimal 31
println("Hex 0x1F = ", hex);
// Octal literal
::std::uint_least32_t oct = 077; // decimal 63
println("Octal 077 = ", oct);
}
Output:
Binary 0b1010 = 10
Hex 0x1F = 31
Octal 077 = 63
Note: These prefixes are part of the literal syntax itself. The values are stored as normal integers, regardless of how they are written.
Printing Numbers in Different Bases
C++ lets you write integer literals in decimal, binary (0b...), hexadecimal (0x...), and octal (0...).
When printing with fast_io, you can control the output base using manipulators.
Unlike std::cout, fast_io does not add prefixes like 0x or 0b automatically — it prints only the digits.
Printing Numbers in Different Bases
C++ lets you write integer literals in decimal, binary (0b...), hexadecimal (0x...), and octal (0...).
When printing with fast_io, you can control the output base using manipulators.
fast_io prints only the digits — it does not add prefixes like 0x or 0b.
The base<N> manipulator supports any base from 2 through 36.
#include <fast_io.h>
#include <cstdint>
int main()
{
using namespace ::fast_io::iomnp;
::std::uint_least32_t x = 255; // our test value
println(
"Decimal: ", x, "\n" // 255
"Hex: ", hex(x), "\n" // ff
"Hex upper: ", hexupper(x), "\n" // FF
"Binary: ", bin(x), "\n" // 11111111
"Binary upper: ", binupper(x), "\n" // 11111111
"Octal: ", oct(x), "\n" // 377
"Octal (literal 8): ", oct(8), "\n" // prints 10
"Base 2: ", base<2>(x), "\n" // 11111111
"Base 8: ", base<8>(x), "\n" // 377
"Base 16: ", base<16>(x), "\n" // ff
"Base 35 upper: ", baseupper<35>(x), "\n" // 7U
"Base 36: ", base<36>(x) // 73
);
}
Note: These manipulators only change how the number is displayed. The stored value remains the same integer regardless of the base used for output.
Output:
Decimal: 255
Hex: ff
Hex upper: FF
Binary: 11111111
Binary upper: 11111111
Octal: 377
Octal (base 8): 10
Base 36: 73
Base 35 upper: 7U
Note: These manipulators only change how the number is displayed. The stored value is always the same integer, regardless of whether you print it in decimal, hex, binary, octal, or another base.