Skip to main content

Type-specific presentation

The final character of a spec picks a presentation, and the legal set of characters depends on the argument's type — this is the page you actually look things up on while writing a format string.

Integers

Type charMeaningExample (255)
d (default)Decimal255
b / BBinary, lowercase/uppercase prefix with #11111111
oOctal377
x / XHex, lowercase/uppercase digitsff / FF
cInterpret as a character codeÿ

Floating point

Type charMeaningExample (1234.5)
f / FFixed notation1234.500000
e / EScientific notation1.234500e+03
g / GShortest of fixed/scientific for the given precision1234.5
a / AHexadecimal floating point0x1.348p+10
(default)Shortest round-trip representation1234.5

Strings and chars

s is the default, unquoted presentation. ? is the debug/escaped presentation: it wraps the string in quotes and escapes control characters and non-printable bytes.

The debug presentation prints an escaped, quoted string — the right thing for logging user input
fmt::format("{:?}", "line1\nline2"); // "\"line1\\nline2\""

When logging a value that might contain newlines, tabs, or other control characters, {:?} makes the boundaries of the string unambiguous in a log line — you can tell where it starts and ends and see embedded whitespace instead of it silently reformatting your log output.

bool

s (the default) prints true/false. d treats the bool as an integer and prints 1/0.

Pointers

p is the only presentation for pointers, and it requires the argument to already be a const void* — a typed pointer must be cast explicitly first (static_cast<const void*>(ptr)).

Mismatches

A presentation type that doesn't apply to the argument is a compile-time error with a checked format string, and a fmt::format_error at runtime otherwise

{:x} on a std::string, or {:f} on a bool, is rejected the same way any other spec/type mismatch is. See Error diagnostics for how to read the error either way it surfaces.

See also