C++ Reference ยท Strings and I/O
C++ format specifications
Reference for the std::format mini-language: fill and align, sign, #, 0, width, precision, locale and type letters for integers, floats, strings and bools.
Everything after the colon in a replacement field is a format specification: {:*^12.3f}. The same mini-language is used by std::format, std::print and println. A specification is optional, and each part of it is optional too, but the parts must appear in a fixed order.
Many parts, such as width and precision, can be replaced by a nested {} so they come from arguments at run time. Which type letters and options are allowed depends on the argument's type: asking for d on a string, for example, is a compile-time error.
replacement field: { arg-id : spec }
spec: [[fill]align] [sign] [#] [0] [width] [.precision] [L] [type]
Try it
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.
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
A standard specification has this grammar. Every part may be left out.
| Part | Meaning |
|---|---|
fill |
Any single character except { or }. Only allowed together with align. Default is a space. |
align |
< left, > right, ^ center. Default: left for strings and characters, right for numbers. |
sign |
+ always show the sign, - only for negatives (default), space for a space in front of non-negatives. Numbers only. |
# |
Alternate form: 0x, 0X, 0b, 0 prefixes for integers; always a decimal point for floats. |
0 |
Pad numbers with zeros after the sign and prefix. Ignored if align is given. |
width |
Minimum field width: a number, or {} / to take it from an integer argument. |
.precision |
Floating point: digits after the point (or significant digits for g). Strings: maximum characters. Not allowed for integers. |
L |
Use the locale's formatting for numbers and bool. |
type |
A single letter that picks the presentation (below). |
Type letters
| Argument | Letters |
|---|---|
| integers | d decimal (default), b B binary, o octal, x X hex, c as a character |
| floating point | f F fixed, e E scientific, g G general, a A hex float; default picks the shortest text that round-trips |
bool |
s text true / false (default), or any integer letter to print 0 / 1 |
char |
c (default), ? debug form, or an integer letter to print the code |
| strings | s (default), ? debug form with quotes and escapes (C++23) |
| pointers | p (the default) |
Errors / undefined behavior: an invalid specification for the argument type is a compile-time error for literal format strings, or a std::format_error exception for run-time format strings. A negative or non-integer dynamic width is also a std::format_error.
Usage notes
- Default float output (
{}) prints the shortest decimal text that converts back to the same value:0.1prints as0.1, not0.1000000000000000055.... Use.17gor similar when you want to show the binary error. - Sign and zero fill.
{:05}of-42gives-0042: the zeros go after the sign. Giving an explicit alignment cancels0. - Truncation. Width never truncates; precision on a string does.
{:.3}of"truncated"istru. - Dynamic fields count as arguments.
{:{}.{}f}consumes the value, then the width, then the precision, in that order when numbering is automatic. #on floats keeps a trailing point:{:#.0f}prints3..- Character vs number.
{}of acharprints the letter;{:d}prints its code.{:c}of anintprints the character with that code (it must fit inchar). - Differences from printf. Letters are close (
x,o,e,g), but there are no length modifiers (l,ll,z), and%s/%dmismatches cannot happen.{:b}has no printf equivalent. - Chrono, ranges and tuples have their own specification grammars (for example
{:%H:%M}for time points and{:n}to drop the brackets around a range).
Related
std::format,std::print, Custom formatters,<iomanip>- Lessons: String formatting, Output