Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

C++  Module 14 – Input/output, files, formatting and time

std::format and std::print in depth

Every option of the C++ format specification, why {} prints 100000.0 as 1e+05, formatting into buffers with format_to_n, and run-time format strings.

  • Intermediate
  • 20 minutes
  • Examples run with Clang 21.0.0, GCC 16.2.0 and Python 3.14.8
  • By MySmartCoPilot
Free lesson, Pro answers: 20 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Write format specifications with fill, alignment, sign,
  • Predict what {} and the floating-point presentation types print for a double
  • Predict the padding a width gives UTF-8 text, and escape strings for logs with {:?}
  • Format into existing buffers with std::format_to, std::format_to_n and std::formatted_size
  • Use std::vformat for format strings known only at run time, and handle std::format_error

Before you start

What you’ll learn · Before you start

What you will learn

  • Write format specifications with fill, alignment, sign,
  • Predict what {} and the floating-point presentation types print for a double
  • Predict the padding a width gives UTF-8 text, and escape strings for logs with {:?}
  • Format into existing buffers with std::format_to, std::format_to_n and std::formatted_size
  • Use std::vformat for format strings known only at run time, and handle std::format_error

Before you start

On this page

std::println("{}", 100000.0) prints 1e+05, and {:>8} gives the six bytes of "₹500" four spaces, not two. Both follow fixed rules of the standard. std::format turns a format string and some values into a std::string, and C++23’s std::print and std::println write the same text straight to the console or a file; std::println also ends the line. Each {} in the format string is a replacement field: A pair of braces in the format string of std::print or std::println, such as {} or {:.2f}. It is replaced by the next value, or by the value its number names, as in {0}. More in “Console I/O in C++: cin, cout, print and println”. What follows a colon inside it, such as >10.2f, is the field’s format specification: The part of a replacement field after its colon, such as >8.2f in {:>8.2f}. It sets the fill character, the alignment, the width, the precision and the type of the value printed. More in “Console I/O in C++: cin, cout, print and println”, and its parts always come in the same order: a fill character and an alignment, a sign, #, 0, a width, a precision, L and a The letter that ends a format specification and says how a value is written: d, x, o or b for the base of an integer, f, e, g or a for the notation of a floating-point number, s for text and ? for escaped text..

The compiler checks every format string written in the code, so an unknown option or a missing argument stops the build. To write into memory you already have, std::format_to and std::format_to_n skip the temporary string, and a A format string that exists only while the program runs, such as a template read from a file. The compiler cannot check it, so it goes through std::vformat, which checks it when it is used and throws std::format_error if it is bad. goes through std::vformat, which checks it then.

A specification’s parts always act in the same order

Every part of a specification can be left out, but the parts that are present must keep this order:

Part Written as What it does
Fill and alignment *^, >, < < puts the value on the left of the field, > on the right and ^ in the centre; a character before it, any but a brace, is the fill
Sign +, -, a space + signs positive numbers too, a space leaves room for a sign, - (the default) signs only negative ones
# #x, #.0f The alternate form: a 0x, 0b or 0 prefix for integers, a decimal point that always stays for floating-point numbers
0 06 Pads with zeros after the sign and the prefix, but only when the specification has no alignment
Width 10, {} The smallest number of columns the field takes; {} reads the width from the next argument
Precision .2, .{} Digits after the point for f and e, significant digits for g, and the most columns a string may take
L L Uses the locale for the decimal point and digit grouping
Type d x b o e f g a c s p ? How the value is written: the base of an integer, the notation of a floating-point number, ? for escaped text

Every example below is built and run with Apple clang 21 and with GCC 16, and the two outputs are shown together. Each line of this program shows one specification and what it printed between brackets, and the table at the end writes four numbers in four bases:

spec_fields.cpp C++ · spec_fields.cpp
// The options of a format specification one at a time, then a table of numbers in four bases.
#include <print>

int main() {
    // {{ and }} print a brace, so each line shows its placeholder and then what it printed
    std::println("{{:*^9}}    [{:*^9}]", "mid");               // centre in 9 columns, pad with *
    std::println("{{:>6}}     [{:>6}]", 42);                   // right-align in 6 columns
    std::println("{{:<6}}     [{:<6}]", 42);                   // left-align
    std::println("{{:+}}      [{:+}] [{:+}]", 7, -7);           // a sign for positive numbers too
    std::println("{{: }}      [{: }] [{: }]", 7, -7);           // a space where the plus would go
    std::println("{{:06}}     [{:06}] [{:06}]", 42, -42);       // zeros after the sign, up to the width
    std::println("{{:<06}}    [{:<06}]", 42);                  // with an alignment, the 0 does nothing
    std::println("{{:#x}}     [{:#x}] [{:#X}] [{:#o}]", 255, 255, 8);
    std::println("{{:#010b}}  [{:#010b}]", 5);                 // the 0b prefix counts towards the 10
    std::println("{{:.3}}     [{:.3}]", "abcdef");             // a precision cuts a string to 3 columns
    std::println("{{:>9.2f}}  [{:>9.2f}]", 3.14159);
    std::println("");

    std::println("{:>8} {:>8} {:>8} {:>18} {:>11}", "decimal", "hex", "octal", "binary", "scientific");
    for (int v : {42, 255, -1024, 65535}) {
        // {0} and {1} pick arguments by position, so one value fills several columns
        std::println("{0:>8} {0:>#8x} {0:>#8o} {0:>#18b} {1:>11.3e}", v, static_cast<double>(v));
    }
}
Each option on its own, then numbers in four bases Python · spec_session.py
from session import run

# spec_fields.cpp built and run with Apple clang, then with GCC 16.
run("""
clang++ -std=c++23 -Wall -Wextra spec_fields.cpp -o spec_fields && ./spec_fields
g++-16 -std=c++23 -Wall -Wextra spec_fields.cpp -o spec_fields-gcc && ./spec_fields-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra spec_fields.cpp -o spec_fields && ./spec_fields
{:*^9}    [***mid***]
{:>6}     [    42]
{:<6}     [42    ]
{:+}      [+7] [-7]
{: }      [ 7] [-7]
{:06}     [000042] [-00042]
{:<06}    [42    ]
{:#x}     [0xff] [0XFF] [010]
{:#010b}  [0b00000101]
{:.3}     [abc]
{:>9.2f}  [     3.14]

 decimal      hex    octal             binary  scientific
      42     0x2a      052           0b101010   4.200e+01
     255     0xff     0377         0b11111111   2.550e+02
   -1024   -0x400   -02000     -0b10000000000  -1.024e+03
   65535   0xffff  0177777 0b1111111111111111   6.554e+04
$ g++-16 -std=c++23 -Wall -Wextra spec_fields.cpp -o spec_fields-gcc && ./spec_fields-gcc
{:*^9}    [***mid***]
{:>6}     [    42]
{:<6}     [42    ]
{:+}      [+7] [-7]
{: }      [ 7] [-7]
{:06}     [000042] [-00042]
{:<06}    [42    ]
{:#x}     [0xff] [0XFF] [010]
{:#010b}  [0b00000101]
{:.3}     [abc]
{:>9.2f}  [     3.14]

 decimal      hex    octal             binary  scientific
      42     0x2a      052           0b101010   4.200e+01
     255     0xff     0377         0b11111111   2.550e+02
   -1024   -0x400   -02000     -0b10000000000  -1.024e+03
   65535   0xffff  0177777 0b1111111111111111   6.554e+04

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 spec_session.py

The two compilers agree on every character. A few results are worth reading twice:

  • Without an alignment, numbers sit on the right of their field and text on the left; {:>6} and {:<6} choose.
  • {:06} puts the zeros after the sign, so -42 becomes -00042. {:<06} prints 42 and four spaces: once an alignment is given, the 0 option does nothing.
  • The prefix counts towards the width: {:#010b} gives 0b00000101, ten characters in all.
  • A negative number keeps its minus sign in every base, so -1024 is -0x400. {:x} of -1 prints -1, not the ffffffff that printf("%x") gives; convert to an unsigned type to see the bits.
  • {0} and {1} choose arguments by position, which lets one value fill four columns. Numbering is all or nothing: a string that mixes {} with {0} does not compile with either compiler.
  • {{ and }} print a single brace, which is how each line printed its own specification.

The options act in a fixed order, which explains every result above:

std::format makes the digits from the type and precision, adds the sign and prefix, then pads to the width or with zeros.std::format("{:>+10.2f}", 3.14159)1. Type and precisionf with .2 rounds to "3.14"2. Sign and #+ writes "+3.14" (# would add0x or 0b, or keep a point)3. Width, fill and alignment> pads on the left to 10columns: 5 spaces, then +3.143. instead, with 0 and no alignment:{:+010.2f} puts zeros afterthe sign: "+000003.14"or

The order in which a format specification acts

Text description of the diagram

The diagram follows std::format("{:>+10.2f}", 3.14159) through the three steps that every replacement field goes through.

  1. The type and the precision make the digits: the type f with the precision .2 rounds 3.14159 to "3.14".
  2. The sign and the # option add what goes in front of the digits: + writes "+3.14". For integers, # would add a prefix such as 0x or 0b; for floating-point types it keeps the decimal point.
  3. The width, the fill character and the alignment pad the whole result: > pads on the left with the fill, a space by default, until the field is 10 columns wide, giving five spaces followed by +3.14.
  4. The other branch of step 3: with the 0 option and no alignment, as in {:+010.2f}, zeros go between the sign and the digits instead, giving "+000003.14". With an alignment, the 0 option does nothing.

The diagram follows one field. Step through three fields of the program the same way, part by part, and watch where the zeros, the sign and the prefix of the What # in a format specification asks for: the 0x, 0X, 0b or 0 prefix of an integer written in another base, or a decimal point that a floating-point number keeps even with no digits after it. end up:

Interactive · step by step Three fields of spec_fields.cpp, built part by part Step through each field. The digits come first, then what goes in front of them, then the padding.
Three fields of spec_fields.cpp, built part by part, step 8 of 8. Showing The field ({:#010b} with 5), 1. Type and precision, 2. Sign and #, 3. Zero, width and fill, Text so far (0b00000101), Printed ([0b00000101]). In focus: 3. Zero, width and fill, Printed.The field{:#010b} with 51. Type and precisionmake the digits2. Sign and #go in front3. Zero, width and fillpad the fieldText so far0b00000101Printed[0b00000101]

The drawing shows the whole scene; its 8 steps are listed below.

Every step as text
  1. {:06} with -42. The first field is {:06} with the value -42. It has no type and no precision, so the digits are the plain decimal ones of the number: 42.
  2. The sign goes in front. The value is negative, so a minus sign goes in front of the digits: -42. Without + or a space in the specification, a positive number would get no sign at all.
  3. Zeros after the sign. 0 with the width 6 and no alignment pads with zeros between the sign and the digits until the field is 6 characters wide: -00042, as the program printed.
  4. {:<06} with 42. The second field, {:<06}, has an alignment as well as the 0. The digits are 42 again, and the number is positive, so nothing goes in front.
  5. The alignment wins. With an alignment, the 0 option does nothing. < puts 42 on the left, and the fill, a space by default, makes up the 6 columns: 42 followed by four spaces.
  6. {:#010b} with 5. The third field writes 5 in binary. The presentation type b makes the digits 101.
  7. # adds the prefix. # asks for the alternate form, which puts 0b in front of binary digits: 0b101, five characters so far.
  8. The prefix counts. 0 and the width 10 put zeros after the prefix until the whole field, prefix included, is 10 characters: 0b00000101. Five zeros were needed, not seven, because 0b counts towards the width.

Key idea

The digits come first, then the sign and the prefix, then the padding: 0 pads with zeros only when no alignment is given, and a prefix counts towards the width.

Checkpoint

Check: The parts of a format specification, in order

  1. Question 1 of 2 What does std::format("{:06}", -42) return?

    Choose one answer.

    A hint

    The sign goes in front of the digits before the field is padded.

    Show the answer: Question 1 of 2

    Answer: -00042

    The digits 42 come first, then the minus sign in front, and then 0 pads the field to its width of 6 with zeros after the sign: -00042, as spec_fields.cpp printed with both compilers.

  2. Question 2 of 2 What does std::format("{:#010b}", 5) return?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: 0b00000101

    The type b makes the digits 101, # puts 0b in front of them, and 0 with the width 10 adds zeros after the prefix until the field has 10 characters: 0b00000101.

A double without a type prints in its shortest exact form

Without a type, a double is written in the shortest round-trip text: The shortest decimal text that reads back as exactly the same floating-point value, which std::format("{}") and std::to_chars print: 0.1 for the double nearest 0.1, where six fixed decimals would print 0.100000 and lose 1e-7 entirely. More in “Parsing strings in C++ with std::from_chars”: the rule that the standard takes from std::to_chars. The same rule chooses between fixed and exponent notation by length alone, and that surprises almost everyone:

floats.cpp C++ · floats.cpp
// What {} prints for a double, and what a type and a precision change.
#include <limits>
#include <print>

int main() {
    // {} prints the shortest text that reads back as exactly the same double ...
    std::println("[{}] [{}] [{}]", 0.1, 0.1 + 0.2, 1.0 / 3);
    // ... and uses exponent form whenever that text is shorter
    std::println("[{}] [{}] [{}] [{}]", 100000.0, 120000.0, 1000000.0, 0.0001);
    // a type and a precision decide instead
    std::println("[{:f}] [{:.2f}] [{:.3e}] [{:g}] [{:.3g}]", 100000.0, 100000.0, 100000.0, 100000.0, 100000.0);
    // rounding works on the stored binary value, which is a little below 2.675
    std::println("[{:.2f}] [{:.20f}]", 2.675, 2.675);
    // # keeps the decimal point; zero has a sign; infinity and NaN have names
    std::println("[{:#.0f}] [{:.0f}] [{}] [{}] [{}]", 2.0, 2.0, -0.0,
                 std::numeric_limits<double>::infinity(), std::numeric_limits<double>::quiet_NaN());
    // a float is printed with the digits a float needs, not a double's
    std::println("[{}] [{}]", 0.1f, static_cast<double>(0.1f));
}
Shortest output, types and precisions Python · floats_session.py
from session import run

# floats.cpp built and run with Apple clang, then with GCC 16.
run("""
clang++ -std=c++23 -Wall -Wextra floats.cpp -o floats && ./floats
g++-16 -std=c++23 -Wall -Wextra floats.cpp -o floats-gcc && ./floats-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra floats.cpp -o floats && ./floats
[0.1] [0.30000000000000004] [0.3333333333333333]
[1e+05] [120000] [1e+06] [1e-04]
[100000.000000] [100000.00] [1.000e+05] [100000] [1e+05]
[2.67] [2.67499999999999982236]
[2.] [2] [-0] [inf] [nan]
[0.1] [0.10000000149011612]
$ g++-16 -std=c++23 -Wall -Wextra floats.cpp -o floats-gcc && ./floats-gcc
[0.1] [0.30000000000000004] [0.3333333333333333]
[1e+05] [120000] [1e+06] [1e-04]
[100000.000000] [100000.00] [1.000e+05] [100000] [1e+05]
[2.67] [2.67499999999999982236]
[2.] [2] [-0] [inf] [nan]
[0.1] [0.10000000149011612]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 floats_session.py

0.1 prints as 0.1 although the stored value is slightly more, because no shorter text reads back as that double; 0.1 + 0.2 needs all 17 digits of 0.30000000000000004. In the second line, 100000.0 became 1e+05, five characters against six for 100000, and 1000000.0 and 0.0001 went the same way, while 120000.0 stayed 120000 because its exponent form is no shorter. A report or an export that must show the digits should say so with a type: {:.0f} or {:.2f}. Paper P3505, adopted for the standard after C++26, lets the size of the number decide instead, so that 100000.0 will print as 100000 there; both libraries above still follow the rule of the shorter text.

Step through the choice for the values of the first two lines, then for the third line, where a type decides:

Interactive · step by step How {} chose fixed or exponent notation in floats.cpp Step through the doubles of the program's first two lines. For each, compare the two ways of writing the same digits.
How {} chose fixed or exponent notation in floats.cpp, step 7 of 7. Showing The double (100000.0), Fewest exact digits (chosen by the type), Fixed notation ({:.2f} gives 100000.00), Exponent notation ({:.3e} gives 1.000e+05), What {} printed (what the type says). In focus: What {} printed.The double100000.0Fewest exact digitschosen by the typeFixed notation{:.2f} gives 100000.00Exponent notation{:.3e} gives 1.000e+05What {} printedwhat the type says

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. The fewest digits. {} first looks for the fewest significant digits that read back as exactly this double. For 100000.0 that is a single digit, 1, followed by the right power of ten.
  2. Two ways to write it. The same digit can be written in fixed notation, 100000, which takes six characters, or in exponent notation, 1e+05, which takes five.
  3. The shorter one wins. The rule takes the shorter text, so {} printed 1e+05 with both libraries. Nothing about the size of the number decides, only the length of the text.
  4. 120000.0. 120000.0 needs two digits, 1 and 2. Fixed notation, 120000, takes six characters and exponent notation, 1.2e+05, takes seven, so this time fixed wins: 120000.
  5. 1000000.0 and 0.0001. Both need one digit. 1e+06 is two characters shorter than 1000000, and 1e-04 one shorter than 0.0001, so both printed in exponent notation.
  6. 0.1 + 0.2. The sum is not the double nearest 0.3, so it needs 17 digits to read back exactly. In fixed notation that is 0.30000000000000004, shorter than its exponent form, and that is what both libraries printed.
  7. A type decides instead. A presentation type switches the choice off. In the third line, {:f} wrote 100000.0 as 100000.000000 and {:.2f} as 100000.00, while {:.3e} asked for exponent notation: 1.000e+05.

The other lines show what a type and a precision change:

  • f and e take the precision as digits after the point (six by default), and g as significant digits, dropping zeros at the end.
  • {:.2f} rounds the value that is stored, and 2.675 is stored as 2.67499999999999982236…, so it prints 2.67, as printf does. Amounts of money are safer as whole numbers of the smallest unit, formatted from those integers, as the exercise below does.
  • # keeps the decimal point (2.), -0.0 keeps its sign, and infinity and NaN print as inf and nan.
  • A float gets the digits a float needs: 0.1f prints as 0.1, and the same value converted to double prints as 0.10000000149011612.
IEEE 754 Floating-Point Converter See the exact binary value of 0.1 or 2.675 and why its decimal digits never end.

Key idea

Without a type, a double prints the fewest digits that read back exactly, in exponent notation whenever that text is shorter; give a presentation type such as {:.2f} or {:g} when the form of the digits matters.

Checkpoint

Check: What a double prints as without a type

  1. Question 1 of 2 Which of these doubles does std::println("{}", x) print in exponent notation? Choose every one.

    Choose every answer that is right.

    A hint

    Write each value both ways and count the characters.

    Show the answer: Question 1 of 2

    Answer:

    • 0.0001
    • 1000000.0

    Without a type, the double is written with the fewest digits that read back exactly, in whichever notation is shorter. The program printed 1e+06 and 1e-04, but 120000 and 0.1.

  2. Question 2 of 2 std::format("{:.2f}", 2.675) returns 2.67, not 2.68. Why?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The double stored for 2.675 is slightly less than 2.675, and that stored value is what gets rounded

    2.675 has no exact binary form, and the double nearest to it is 2.67499999999999982236…, so two decimals give 2.67. Amounts of money are safer as whole numbers of the smallest unit, such as paise.

Widths count columns on the screen, not bytes

For text, the width and the precision count columns on the screen, not bytes. The standard asks for an estimate: it treats each grapheme cluster: What a reader sees as one character, which may be several code points, such as e followed by a combining accent, or the conjunct स्ते. Unicode Standard Annex #29 defines where clusters begin and end. More in “UTF-8, Unicode and text encodings in C++”, the unit Unicode calls a user-perceived character, as one column, and wide East Asian characters and most emoji as two:

text.cpp C++ · text.cpp
// Strings and other values: what a width counts, escaped output, and ranges.
#include <format>
#include <map>
#include <print>
#include <string>
#include <string_view>
#include <vector>

int main() {
    // A width counts columns on the screen, not bytes.
    for (std::string_view s : {"abc", "€20", "₹500", "नमस्ते", "😀"}) {
        std::println("[{:>8}] is {:>2} bytes, {:>2} bytes padded", s, s.size(), std::formatted_size("{:>8}", s));
    }
    // ? prints a string or a character the way you would write it in code.
    std::println("[{}] [{:?}] [{:?}]", "tab\tand\nnewline", "tab\tand\nnewline", '\'');
    // bool, char, pointers
    std::println("[{}] [{:d}] [{}] [{:d}] [{:c}] [{}]", true, true, 'A', 'A', 66, nullptr);
    // C++23 formats ranges and maps, with a specification for each element after a second colon.
    std::vector<int> codes{10, 255, 7};
    std::map<std::string, int> stock{{"pens", 40}, {"ink", 3}};
    std::vector<std::string> names{"Asha Rao", "Ravi"};
    std::println("[{}] [{:n}] [{::#x}] [{}] [{}]", codes, codes, codes, stock, names);
}
Widths, escaped strings, other types and ranges Python · text_session.py
from session import run

# text.cpp built and run with Apple clang, then with GCC 16.
run("""
clang++ -std=c++23 -Wall -Wextra text.cpp -o text && ./text
g++-16 -std=c++23 -Wall -Wextra text.cpp -o text-gcc && ./text-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra text.cpp -o text && ./text
[     abc] is  3 bytes,  8 bytes padded
[     €20] is  5 bytes, 10 bytes padded
[    ₹500] is  6 bytes, 10 bytes padded
[     नमस्ते] is 18 bytes, 23 bytes padded
[      😀] is  4 bytes, 10 bytes padded
[tab	and
newline] ["tab\tand\nnewline"] ['\'']
[true] [1] [A] [65] [B] [0x0]
[[10, 255, 7]] [10, 255, 7] [[0xa, 0xff, 0x7]] [{"ink": 3, "pens": 40}] [["Asha Rao", "Ravi"]]
$ g++-16 -std=c++23 -Wall -Wextra text.cpp -o text-gcc && ./text-gcc
[     abc] is  3 bytes,  8 bytes padded
[     €20] is  5 bytes, 10 bytes padded
[    ₹500] is  6 bytes, 10 bytes padded
[     नमस्ते] is 18 bytes, 23 bytes padded
[      😀] is  4 bytes, 10 bytes padded
[tab	and
newline] ["tab\tand\nnewline"] ['\'']
[true] [1] [A] [65] [B] [0x0]
[[10, 255, 7]] [10, 255, 7] [[0xa, 0xff, 0x7]] [{"ink": 3, "pens": 40}] [["Asha Rao", "Ravi"]]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 text_session.py

"₹500" is 6 bytes in UTF-8 but 4 columns wide, so {:>8} added four spaces, and std::formatted_size reported the 10 bytes of the result. The Hindi word नमस्ते is 18 bytes and, by both libraries’ estimate, 3 columns: the conjunct स्ते at its end, four code points, forms one cluster. The estimate is only an estimate, though, because terminals and fonts draw scripts such as Devanagari in different widths, so columns of such text may still not line up on every screen. The emoji counts as two columns.

The five lines of the run, side by side: the spaces are what the width of 8 leaves after the columns the text takes, and the bytes of the padded result are the text’s bytes plus those spaces.

Text Bytes in UTF-8 Columns Spaces before it
abc 3 3 5
€20 5 3 5
₹500 6 4 4
नमस्ते 18 3 5
😀 4 2 6

The rest of the output covers the other types:

  • {:?} writes a string or a character the way you would write it in code: quoted, with \t, \n and quote marks escaped. It is the right choice for log lines, where a hidden tab or line break would otherwise vanish.
  • bool prints as true, or as 1 with {:d}; a char prints as a character, or as its code with {:d}, and an integer prints as a character with {:c}. Pointers print in hexadecimal, and nullptr as 0x0.
  • C++23 formats ranges: a vector prints with brackets and commas, {:n} drops the brackets, and a specification after a second colon applies to every element, so {::#x} writes each code in hexadecimal. Maps print as {key: value} pairs, and strings inside a range are quoted and escaped.
Unicode Character Inspector Look up the code points and byte lengths of any text before you decide on a width.

Checkpoint

Check: Widths of text, and escaped output

  1. Question 1 of 2 नमस्ते is 18 bytes in UTF-8 and, by the libraries' estimate, 3 columns wide. How many spaces does std::format("{:>8}", text) put in front of it?

    Type a number.

    A hint

    The width counts columns on the screen, not bytes.

    Show the answer: Question 1 of 2

    Answer: 5 spaces

    The width is 8 columns and the text takes 3, so the field gets 8 − 3 = 5 spaces in front. The result is 18 + 5 = 23 bytes, which is what std::formatted_size reported in the program.

  2. Question 2 of 2 A log line must show a string that holds a tab so that the tab can be seen as \t. Which field writes it that way?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: {:?}

    The ? type of C++23 writes escaped text: text.cpp printed "tab\tand\nnewline" in quotes, while {} printed the tab and the line break themselves.

Formatting into memory you already have saves allocations

std::format returns a new string every time. Three functions let you decide where the characters go:

buffers.cpp C++ · buffers.cpp
// Formatting into memory you already have: format_to_n, formatted_size and format_to.
#include <cstddef>
#include <cstdlib>
#include <format>
#include <iterator>
#include <new>
#include <print>
#include <string>
#include <string_view>

// Every allocation goes through operator new, so counting here shows which calls allocate.
static long allocations = 0;
void* operator new(std::size_t n) {
    ++allocations;
    if (void* p = std::malloc(n ? n : 1)) return p;
    throw std::bad_alloc{};
}
void operator delete(void* p) noexcept { std::free(p); }
void operator delete(void* p, std::size_t) noexcept { std::free(p); }

int main() {
    // 1. A fixed buffer: format_to_n writes at most n characters and reports how many the whole result has.
    char buf[12];
    auto r = std::format_to_n(buf, sizeof buf, "Total: {:.2f}", 1234567.891);
    std::string_view written(buf, static_cast<std::size_t>(r.out - buf));
    std::println("wrote [{}]: {} of {} characters{}", written, written.size(), r.size,
                 r.size > std::ssize(buf) ? ", the rest was cut" : "");

    // 2. formatted_size: the room a result needs, before writing it.
    std::println("formatted_size: {}", std::formatted_size("Total: {:.2f}", 1234567.891));

    // 3. One string reused for every row: no allocation once it has the capacity.
    std::string row;
    row.reserve(64);
    long before = allocations;
    for (int i = 0; i < 1000; ++i) {
        row.clear();
        std::format_to(std::back_inserter(row), "row {:>4} {:>10.2f} and a longer label", i, i * 1.5);
    }
    long reused = allocations - before;

    // 4. A new string for every row: one allocation each, once a row is too long for the string itself.
    before = allocations;
    std::string made;
    for (int i = 0; i < 1000; ++i) made = std::format("row {:>4} {:>10.2f} and a longer label", i, i * 1.5);
    long fresh = allocations - before;

    std::println("1000 rows: format_to into one string allocated {} times, std::format {} times", reused, fresh);
    std::println("a std::string holds up to {} characters without allocating", std::string().capacity());
    std::println("last row: [{}]", row);
}
A fixed buffer, the size needed, and a counter of allocations Python · buffers_session.py
from session import run

# buffers.cpp built without optimisation (so the allocation counter sees every call) and run with both compilers.
run("""
clang++ -std=c++23 -Wall -Wextra buffers.cpp -o buffers && ./buffers
g++-16 -std=c++23 -Wall -Wextra buffers.cpp -o buffers-gcc && ./buffers-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra buffers.cpp -o buffers && ./buffers
wrote [Total: 12345]: 12 of 17 characters, the rest was cut
formatted_size: 17
1000 rows: format_to into one string allocated 0 times, std::format 1000 times
a std::string holds up to 22 characters without allocating
last row: [row  999    1498.50 and a longer label]
$ g++-16 -std=c++23 -Wall -Wextra buffers.cpp -o buffers-gcc && ./buffers-gcc
wrote [Total: 12345]: 12 of 17 characters, the rest was cut
formatted_size: 17
1000 rows: format_to into one string allocated 0 times, std::format 1000 times
a std::string holds up to 15 characters without allocating
last row: [row  999    1498.50 and a longer label]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 buffers_session.py

  • std::format_to_n(out, n, …) writes at most n characters and returns a pair: out, where the writing stopped, and size, the length the whole result has. Here 12 characters went into the 12-character buffer, and size was 17, so the caller knows the end was cut. It never writes a terminating '\0'; add one yourself when a C function will read the buffer.
  • std::formatted_size(…) returns that length without storing anything, so you can check that a result fits before you write it.
  • std::format_to(out, …) writes through any output iterator. With std::back_inserter and one string that is cleared and reused, a thousand rows caused no allocation at all, while std::format allocated once per row on both libraries. Each row was too long to fit small-string optimisation: Storing the characters of a short string inside the std::string object itself, so that no heap memory is allocated. The limit is the library's own: 22 characters with libc++ and 15 with libstdc++ in this track's 64-bit builds. More in “std::string and std::string_view”, which holds 22 characters with libc++ and 15 with libstdc++ before it needs memory of its own.

Step through the first two parts of the program, the call that fills the 12-character buffer and the one that only measures:

Interactive · message by message format_to_n filling a 12-character buffer in buffers.cpp Step through the call. Watch how many characters reach the buffer, and what the call tells the program afterwards.
format_to_n filling a 12-character buffer in buffers.cpp, step 7 of 7. Messages so far: The program to std::format_to_n: buf, 12, Total: {:.2f}; std::format_to_n to char buf[12]: Total: 12345; std::format_to_n to The program: out = buf + 12, size 17.The program17 is more than 12std::format_to_nformatted_size: 17char buf[12]Total: 12345, fullbuf, 12, Total: {:.2f}Total: 12345out = buf + 12, size 17

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. The call. The program asks for "Total: {:.2f}" with 1234567.891, written into buf, at most 12 characters.
  2. The whole text. The full result would be Total: 1234567.89, 17 characters: the 7 of Total: and 10 for the number with two decimals.
  3. It stops at 12. It writes the first 12 characters, Total: 12345, and stops there. Nothing beyond the 12th character of the buffer is touched.
  4. No terminating zero. It never writes a terminating '\0'. A C function that reads buf as a string needs one, so the program must add it, and leave room for it.
  5. Where it stopped, and the size. It returns a pair: out, which points just past the last character written, here buf + 12, and size, the length the whole result has: 17.
  6. The program sees the cut. size is larger than the buffer, 17 against 12, so the program knows that the end was cut and printed the rest was cut.
  7. Ask before writing. std::formatted_size with the same format string and value returned 17 without storing anything, so a program can check that a result fits before it writes.

Allocations rarely matter for a few lines of output. They matter in a logger that formats hundreds of thousands of lines a second, in a game’s frame loop, and in embedded code that may not use the heap at all.

Checkpoint

Check: Formatting into memory you already have

  1. Question 1 of 2 The result of "Total: {:.2f}" with 1234567.891 has 17 characters. How many characters does std::format_to_n(buf, 12, "Total: {:.2f}", 1234567.891) write into buf?

    Type a number.

    A hint

    The second argument is the most it may write.

    Show the answer: Question 1 of 2

    Answer: 12 characters

    format_to_n writes at most n characters, so 12, "Total: 12345", and reports the 17 the whole result has in the size of its return value. It writes no terminating zero.

  2. Question 2 of 2 A C function will read buf as a string after std::format_to_n has filled it. What must the program add?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: A terminating '\0' where the writing stopped, at r.out, with room left for it

    format_to_n writes at most n characters and stops, with no terminator. Leave room for one more character, write at most n − 1, and put the zero at r.out.

std::print and std::println write to a FILE* or any stream

std::print(fmt, args…) writes to standard output, and std::println adds a line break. Both take the same format strings as std::format, and both have overloads for other destinations:

  • std::print(stderr, …) writes to any C FILE*, which is how error messages reach standard error.
  • std::print(std::cout, …), declared in <ostream>, writes to any output stream, such as a file opened with std::ofstream.
  • In C++26, std::println() with no arguments prints an empty line; both compilers accept it with -std=c++26.

When the output is a terminal and the program’s text is UTF-8, std::print writes through the system’s Unicode interface, which is what makes such text appear correctly in a Windows console; the lesson on console input and output shows the build flags for that.

Step through the four kinds of call and where each one sends its text:

Interactive · step by step Where std::print and std::println send their text Step through the four calls. Each one appears with the place its text goes.
Where std::print and std::println send their text, step 5 of 5. Showing std::println(fmt, …), std::print(stderr, …), std::println(out, …), std::println(), Standard output, Standard error, The file out opened, An empty line. In focus: Standard output.std::println(fmt, …)or std::print, no line breakstd::print(stderr, …)any C FILE* firststd::println(out, …)out is an std::ofstreamstd::println()C++26, no argumentsStandard outputthe screen, or > a fileStandard errorstays on the screenThe file out openeddeclared in <ostream>An empty lineon standard output

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. Standard output. std::println(fmt, args…) formats exactly like std::format and writes the text and a line break to standard output. std::print writes the same text without the line break.
  2. A C FILE*. Given a C FILE* first, the same calls write there. With stderr, an error message reaches standard error, which stays on the screen when the normal output goes to a file.
  3. Any output stream. Given an output stream first, such as an std::ofstream named out, the line goes into that stream. These overloads are declared in <ostream>, not in <print>.
  4. An empty line. C++26 adds std::println() with no arguments, which writes only a line break to standard output, the same as std::println("").
  5. Text beyond ASCII. When standard output is a terminal and the program's text is UTF-8, std::print writes through the system's Unicode interface, which is what makes such text appear correctly in a Windows console.

Checkpoint

Check: Where std::print and std::println write

  1. Question 1 of 2 Which call writes an error message to standard error?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: std::println(stderr, "cannot open {}", path)

    std::print and std::println write to standard output, or to the C FILE* or the output stream given first. Error messages belong on standard error, so they stay apart from the results.

  2. Question 2 of 2 A program calls std::println(out, "{}", total), where out is an std::ofstream. Which header declares that overload?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: <ostream>

    std::println(std::cout, …) and std::println(out, …) for any output stream come from <ostream>; the console and FILE* versions come from <print>.

A format string read at run time is checked when it is used

std::format("{} of {}", a, b) compiles only because the compiler can read the format string: a constructor that runs while compiling checks it against the types of the arguments. A string read from a file, a translation catalogue or a database is not available then, so it goes to std::vformat, with the arguments packed by std::make_format_args:

runtime.cpp C++ · runtime.cpp
// Format strings that exist only while the program runs: std::vformat and std::make_format_args.
#include <format>
#include <print>
#include <string>
#include <string_view>

// make_format_args stores references to its arguments, so they must be variables that outlive the call.
std::string fill_in(std::string_view templ, int count, std::string_view item) {
    return std::vformat(templ, std::make_format_args(count, item));
}

int main() {
    std::println("{}", fill_in("{} x {}", 3, "pen"));
    std::println("{}", fill_in("{1}: {0:>4}", 12, "ink"));
    for (std::string_view bad : {"{} x {} x {}", "{:q}", "{"}) {
        try {
            std::println("{}", fill_in(bad, 1, "x"));
        } catch (const std::format_error& e) {
            std::println("{:<12} -> std::format_error: {}", bad, e.what());
        }
    }
    // A width or precision chosen at run time needs no vformat: a nested {} takes it from the arguments.
    int width = 9;
    int digits = 2;
    std::println("[{:>{}}] [{:^{}.{}f}]", "right", width, 3.14159, width, digits);
}
dynamic.cpp C++ · dynamic.cpp
// C++26: mark a format string as known only at run time and pass it to std::println directly.
#include <format>
#include <print>
#include <string>

int main() {
    std::string templ = "{} of {} files copied";
#if __cpp_lib_format >= 202603L
    std::println(std::dynamic_format(templ), 3, 5);   // the name in C++26 (GCC 16)
#else
    std::println(std::runtime_format(templ), 3, 5);   // the earlier name (Apple clang 21)
#endif
    std::println("__cpp_lib_format is {}", __cpp_lib_format);
}
Run-time format strings in C++23, then with the C++26 marker Python · runtime_session.py
from session import run

# runtime.cpp with both compilers in C++23 mode, then dynamic.cpp in C++26 mode.
run("""
clang++ -std=c++23 -Wall -Wextra runtime.cpp -o runtime && ./runtime
g++-16 -std=c++23 -Wall -Wextra runtime.cpp -o runtime-gcc && ./runtime-gcc
clang++ -std=c++26 -Wall -Wextra dynamic.cpp -o dynamic && ./dynamic
g++-16 -std=c++26 -Wall -Wextra dynamic.cpp -o dynamic-gcc && ./dynamic-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra runtime.cpp -o runtime && ./runtime
3 x pen
ink:   12
{} x {} x {} -> std::format_error: The argument index value is too large for the number of arguments supplied
{:q}         -> std::format_error: The format specifier should consume the input or end with a '}'
{            -> std::format_error: The format string terminates at a '{'
[    right] [  3.14   ]
$ g++-16 -std=c++23 -Wall -Wextra runtime.cpp -o runtime-gcc && ./runtime-gcc
3 x pen
ink:   12
{} x {} x {} -> std::format_error: format error: invalid arg-id in format string
{:q}         -> std::format_error: format error: failed to parse format-spec
{            -> std::format_error: format error: unmatched '{' in format string
[    right] [  3.14   ]
$ clang++ -std=c++26 -Wall -Wextra dynamic.cpp -o dynamic && ./dynamic
3 of 5 files copied
__cpp_lib_format is 202110
$ g++-16 -std=c++26 -Wall -Wextra dynamic.cpp -o dynamic-gcc && ./dynamic-gcc
3 of 5 files copied
__cpp_lib_format is 202603

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 runtime_session.py

Step through the strings of runtime.cpp, the templates that fill_in() receives and the literal with a nested width, and see where each one is checked:

Interactive · step by step Where the format strings of runtime.cpp are checked Step through the program's strings. Those written in the code are checked while compiling; the others only when they are used.
Where the format strings of runtime.cpp are checked, step 6 of 6. Showing A string in the code, Checked while compiling, Width from an argument (printed [ right]), A template at run time, std::vformat (gave ink: 12), std::format_error (all three threw). In focus: Width from an argument, Checked while compiling.A string in the codestd::format("{} of {}", …)Checked while compilinga bad one stops the buildWidth from an argumentprinted [ right]A template at run timefrom a file or a cataloguestd::vformatgave ink: 12std::format_errorall three threw

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. Checked by the compiler. A format string written in the code is read by the compiler: a constructor that runs while compiling checks every field against the types of the arguments, so a mistake stops the build.
  2. A template from outside. fill_in() receives its template as a std::string_view parameter, a value known only at run time, as a template read from a file would be. It goes to std::vformat with the arguments packed by std::make_format_args: "{} x {}" gave 3 x pen.
  3. By position. "{1}: {0:>4}" picks the arguments by number and gives the count a width of 4: ink: 12. Valid templates work exactly as literals do.
  4. {} x {} x {}. This template asks for three arguments and gets two. The check runs when std::vformat reads the string, and it throws std::format_error.
  5. {:q} and a lone {. q is no presentation type, and a string that ends in { ends inside a field, so both threw as well. The two libraries word their messages differently, so code catches the type, never the text.
  6. A width at run time. A width or a precision chosen while the program runs needs no vformat: in {:>{}} the inner braces take the width from the next argument, so [ right] came from a string the compiler still checked.

Three things show in the output:

  1. A bad format string is reported when it is used, as std::format_error. The two libraries word their messages differently, so code should catch the type and never test the text.
  2. std::make_format_args keeps references to its arguments, so they must be variables that live until the call returns. Passing a temporary, such as a std::string built in the call, does not compile with either library: paper P2905 made that a compile error because the dangling reference: A reference whose object has already been destroyed, such as one returned from a function to its own local variable. Reading or writing through it is undefined behaviour, even when the program happens to print the right value. More in “C++ references: lifetime extension and dangling”.
  3. A width or precision chosen at run time needs no vformat. A nested {} takes it from the next argument, as the last line did with {:>{}} and {:^{}.{}f}, and the format string stays checked while compiling.

C++26: dynamic_format, formerly runtime_format

C++26 lets a run-time string go straight to std::format and std::println once it is marked. Paper P2918 named the marker std::runtime_format, and paper P3953, adopted for C++26, renamed it std::dynamic_format, because a format string can now also be formatted while compiling. GCC 16 has the new name and reports __cpp_lib_format as 202603; the libc++ of Apple clang 21 still has only std::runtime_format, which is why dynamic.cpp tests the macro.

A format string written by a user deserves the care of any other input. Being valid does not make it harmless: {:>1000000000} is a correct specification, and filling it in builds a gigabyte of spaces. Check a template when it is saved, allow only the fields and the widths you expect, and catch std::format_error wherever templates are filled in.

Checkpoint

Check: Format strings known only at run time

  1. Question 1 of 2 A column's width is read from a settings file when the program starts. How do you use it and keep the format string checked while compiling?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: A nested field: std::format("{:>{}}", name, width)

    A nested replacement field such as {:>{}} or {:^{}.{}f} takes the width and the precision from the arguments, as runtime.cpp did, and needs no vformat.

  2. Question 2 of 2 std::vformat(templ, std::make_format_args(count, std::string("pen"))) does not compile. Why?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: make_format_args keeps references to its arguments, and the temporary string would be gone before vformat used it

    The packed arguments refer to the variables passed in, so they must live until the call returns. Store the string in a variable first, as fill_in() does with its parameters.

Practise: a bank statement printer

statement.h declares three functions for printing a bank statement; amounts are kept in paise, 100 to the rupee, so no floating-point rounding can creep in. Complete them:

  1. amount(paise) writes rupees, a point and exactly two digits of paise: 123456 gives 1234.56, 1005 gives 10.05, and -50 gives -0.50.
  2. statement_line(entry, balance) writes the date, two spaces, the description left-aligned in 20 columns and cut if it is longer, two spaces, the amount right-aligned in 10 columns, two spaces and the balance right-aligned in 12.
  3. closing_line(balance) writes Closing balance left-aligned in 34 columns and the balance right-aligned in 22.
The starter code and its sample tests

C header · statement.h

// A bank statement printer. Complete amount(), statement_line() and closing_line(); the tests are in statement_tests.cpp.
#pragma once
#include <format>
#include <string>
#include <string_view>

struct Entry {
    std::string_view date;          // for example "2025-04-03"
    std::string_view description;   // shown in at most 20 columns
    long long paise;                // 100 paise make a rupee; money in is positive, money out negative
};

// The amount in rupees with exactly two decimals: 123456 -> "1234.56", -50 -> "-0.50".
inline std::string amount(long long paise) {
    return std::to_string(paise);   // TODO: rupees, a point and two digits of paise
}

// One row: the date, two spaces, the description left-aligned in 20 columns (cut if longer), two spaces,
// the amount right-aligned in 10 columns, two spaces and the balance after this entry right-aligned in 12.
// Example: "2025-04-03  Rent                   -15000.00       9500.00"
inline std::string statement_line(const Entry& e, long long balance_paise) {
    return std::format("{} {} {} {}", e.date, e.description, amount(e.paise), amount(balance_paise));   // TODO
}

// The last line: "Closing balance" left-aligned in 34 columns, then the balance right-aligned in 22.
inline std::string closing_line(long long balance_paise) {
    return std::format("Closing balance {}", amount(balance_paise));   // TODO
}

C++ · statement_tests.cpp

// Sample tests for statement.h: each check compares the exact text, as a judge compares output.
#include <print>
#include <string>
#include <string_view>
#include "statement.h"

int passed = 0, total = 0;

void check(std::string_view what, const std::string& got, std::string_view want) {
    ++total;
    if (got == want) ++passed;
    else std::println("FAIL {}\n  got  [{}]\n  want [{}]", what, got, want);
}

int main() {
    check("amount with paise", amount(123456), "1234.56");
    check("amount under one rupee, negative", amount(-50), "-0.50");
    check("amount with a zero digit of paise", amount(1005), "10.05");
    check("a row", statement_line({"2025-04-03", "Rent", -1500000}, 950000),
          "2025-04-03  Rent                   -15000.00       9500.00");
    check("a long description is cut to 20 columns",
          statement_line({"2025-04-05", "Electricity bill for March", -184250}, 765750),
          "2025-04-05  Electricity bill for    -1842.50       7657.50");
    check("a description with an accent takes its columns, not its bytes",
          statement_line({"2025-04-09", "Café Mocha", -32000}, 733750),
          "2025-04-09  Café Mocha               -320.00       7337.50");
    check("the closing line is 56 columns wide",
          closing_line(733750), "Closing balance                                  7337.50");
    std::println("{} of {} checks passed", passed, total);
    return passed == total ? 0 : 1;
}

The tests compare every character, the way an online judge compares output. Against the starter code, all seven fail:

The sample tests against the starter code Python · statement_session.py
from session import run

# The sample tests against the starter code, with both compilers.
run("""
clang++ -std=c++23 -Wall -Wextra statement_tests.cpp -o statement_tests && ./statement_tests
g++-16 -std=c++23 -Wall -Wextra statement_tests.cpp -o statement_tests-gcc && ./statement_tests-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra statement_tests.cpp -o statement_tests && ./statement_tests
FAIL amount with paise
  got  [123456]
  want [1234.56]
FAIL amount under one rupee, negative
  got  [-50]
  want [-0.50]
FAIL amount with a zero digit of paise
  got  [1005]
  want [10.05]
FAIL a row
  got  [2025-04-03 Rent -1500000 950000]
  want [2025-04-03  Rent                   -15000.00       9500.00]
FAIL a long description is cut to 20 columns
  got  [2025-04-05 Electricity bill for March -184250 765750]
  want [2025-04-05  Electricity bill for    -1842.50       7657.50]
FAIL a description with an accent takes its columns, not its bytes
  got  [2025-04-09 Café Mocha -32000 733750]
  want [2025-04-09  Café Mocha               -320.00       7337.50]
FAIL the closing line is 56 columns wide
  got  [Closing balance 733750]
  want [Closing balance                                  7337.50]
0 of 7 checks passed
[exit status 1]
$ g++-16 -std=c++23 -Wall -Wextra statement_tests.cpp -o statement_tests-gcc && ./statement_tests-gcc
FAIL amount with paise
  got  [123456]
  want [1234.56]
FAIL amount under one rupee, negative
  got  [-50]
  want [-0.50]
FAIL amount with a zero digit of paise
  got  [1005]
  want [10.05]
FAIL a row
  got  [2025-04-03 Rent -1500000 950000]
  want [2025-04-03  Rent                   -15000.00       9500.00]
FAIL a long description is cut to 20 columns
  got  [2025-04-05 Electricity bill for March -184250 765750]
  want [2025-04-05  Electricity bill for    -1842.50       7657.50]
FAIL a description with an accent takes its columns, not its bytes
  got  [2025-04-09 Café Mocha -32000 733750]
  want [2025-04-09  Café Mocha               -320.00       7337.50]
FAIL the closing line is 56 columns wide
  got  [Closing balance 733750]
  want [Closing balance                                  7337.50]
0 of 7 checks passed
[exit status 1]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 statement_session.py

Hint: build the amount’s text first, with the sign written by hand and the paise as {:02}, then give that finished text its width in statement_line(); a precision on the description’s field cuts it to 20 columns. When both compilers print 7 of 7 checks passed, add a line with a description in Devanagari or with an emoji to the tests and predict its padding before you run them.

Text Diff Paste your output and the expected line to see the exact character where they differ.

Six ideas, from the parts of a specification to templates read at run time, are covered by the questions below. Whichever idea you have got wrong most often comes first, and the feedback on every option names the slip behind it:

Practice that adapts to you

Practise this lesson

17 questions about 6 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 17 Put the parts of a format specification in the order they must be written.

    Give each item its position, from 1 (first).

    Show the answer: Question 1 of 17

    Answer:

    1. Fill and alignment, such as *>
    2. Sign: +, - or a space
    3. #, the alternate form
    4. 0, padding with zeros
    5. Width, such as 10
    6. Precision, such as .2
    7. L, the locale
    8. Presentation type, such as f

    Every part may be left out, but those present keep this order: [[fill]align][sign][#][0][width][.precision][L][type].

  2. Question 2 of 17 What does std::format("{:<06}", 42) return?

    Choose one answer.

    Show the answer: Question 2 of 17

    Answer: 42 followed by four spaces: the alignment wins and the 0 does nothing

    The 0 option pads with zeros only when the specification has no alignment. With < the field is padded with the fill character, a space by default, after the number.

  3. Question 3 of 17 What does this line print?

    Read the code, then choose one answer.

    std::println("{:x} {:x}", -1, static_cast<unsigned>(-1));
    Show the answer: Question 3 of 17

    Answer: -1 ffffffff

    std::format writes a negative number in any base with a minus sign followed by its magnitude, unlike printf's %x, which prints the bit pattern. Converting to an unsigned type first shows the 32 bits of -1, all ones.

  4. Question 4 of 17 Account ids must print as 8 hexadecimal digits with leading zeros after a 0x prefix, such as 0x0000ff3a. Which field does it?

    Choose one answer.

    Show the answer: Question 4 of 17

    Answer: {:#010x}

    # adds the prefix, 0 pads with zeros after it, and the width counts the whole field: {:#010x}. It is the same rule that made {:#010b} print 0b00000101 for 5.

  5. Question 5 of 17 What does std::println("{}", 100000.0) print with both libraries?

    Choose one answer.

    Show the answer: Question 5 of 17

    Answer: 1e+05

    Without a type, a double is printed in its shortest form that reads back as the same value, and when the exponent form is shorter it wins: 1e+05 has five characters, 100000 has six. Write {:.0f} or {:f} to get the digits written out.

  6. Question 6 of 17 std::println("{}", 0.1 + 0.2) prints 0.30000000000000004. Why so many digits?

    Choose one answer.

    Show the answer: Question 6 of 17

    Answer: The sum is not the double nearest 0.3, and 17 digits are the fewest that read back as exactly that sum

    0.1 and 0.2 are stored as the nearest doubles, and their sum lands on a double just above the one nearest 0.3. The shortest text that reads back as that sum has 17 digits.

  7. Question 7 of 17 A label must show the double 100000.0 as 100000. Which field prints exactly that?

    Choose one answer.

    Show the answer: Question 7 of 17

    Answer: {:g}

    The third line of floats.cpp printed 100000.0 as 100000.000000 with {:f}, 100000 with {:g} and 1e+05 with {:.3g}. A presentation type decides the notation, so the result no longer depends on which text is shorter.

  8. Question 8 of 17 What does the width of a field count for UTF-8 text?

    Choose one answer.

    Show the answer: Question 8 of 17

    Answer: Columns on the screen, estimated from grapheme clusters

    For text, the width and the precision count columns. Each grapheme cluster counts as one column, and wide characters and most emoji as two, which is why the emoji got 6 spaces from {:>8}.

  9. Question 9 of 17 "₹500" is 6 bytes in UTF-8. How many spaces does std::format("{:>8}", "₹500") put in front of it?

    Choose one answer.

    Show the answer: Question 9 of 17

    Answer: Four, because the width counts columns and the rupee sign takes one

    For UTF-8 text the width is an estimate of the columns the text takes on screen, so "₹500" counts as 4 columns and gets 4 spaces. The result is 10 bytes long, which std::formatted_size reports.

  10. Question 10 of 17 Which function tells you how many characters a result will have, without writing it anywhere?

    Choose one answer.

    Show the answer: Question 10 of 17

    Answer: std::formatted_size

    std::formatted_size takes the same format string and arguments as std::format and returns the length of the result, so a program can check that a result fits before writing it.

  11. Question 11 of 17 r = std::format_to_n(buf, 12, "Total: {:.2f}", 1234567.891) writes into a 12-character buffer. The full result would be 17 characters. What is r.size?

    Choose one answer.

    Show the answer: Question 11 of 17

    Answer: 17, the size of the whole result

    format_to_n writes at most n characters, and r.size is always the size the whole result would have. Comparing r.size with the buffer's size tells you whether anything was cut; r.out points just past the last character written.

  12. Question 12 of 17 A function fills a fixed 64-character field of a message and must report when the text did not fit. Which call suits it?

    Choose one answer.

    Show the answer: Question 12 of 17

    Answer: std::format_to_n(field, 64, …), then compare the returned size with 64

    format_to_n is the call for memory of a fixed size: it stops at n characters and reports the full size, so the caller can detect the cut, as buffers.cpp did with its 12-character buffer.

  13. Question 13 of 17 What is the difference between std::print and std::println?

    Choose one answer.

    Show the answer: Question 13 of 17

    Answer: std::println ends the text with a line break; std::print does not

    std::print writes the formatted text and std::println adds a line break, both to standard output, a C FILE* or an output stream given first.

  14. Question 14 of 17 A tool writes progress lines with std::print(stderr, …) and its results with std::println(…). A user runs it as ./tool > out.txt. Where do the progress lines appear?

    Choose one answer.

    Show the answer: Question 14 of 17

    Answer: On the screen, because only standard output goes to out.txt

    Writing problems and progress to standard error keeps them apart from the results, so a user can save the results and still see the messages.

  15. Question 15 of 17 What does std::vformat do when the format string it is given is not valid?

    Choose one answer.

    Show the answer: Question 15 of 17

    Answer: It throws std::format_error

    The three bad templates of runtime.cpp each threw std::format_error with both libraries, with messages worded differently, so code catches the type and never compares the text.

  16. Question 16 of 17 A program reads the format string "{} of {}" from a settings file into a std::string fmt. Which call fills it in with C++23?

    Choose one answer.

    Show the answer: Question 16 of 17

    Answer: std::vformat(fmt, std::make_format_args(done, total))

    std::format and std::print check their format string while compiling, so it must be a constant expression; a string from a file is not. std::vformat takes the string at run time and throws std::format_error if it is bad. In C++26, std::format(std::dynamic_format(fmt), done, total) does the same.

  17. Question 17 of 17 Users may save their own receipt templates, which the program later fills in with std::vformat. What should happen when a template is saved?

    Choose one answer.

    Show the answer: Question 17 of 17

    Answer: Check it: only the expected fields, and widths up to a limit, so that {:>1000000000} is refused

    A format string written by a user deserves the care of any other input: check it when it is saved, allow only the fields and widths you expect, and catch std::format_error wherever templates are filled in.

Interview questions

Fresher · When would you use std::format_to or std::format_to_n instead of std::format?

std::format returns a new std::string, which needs memory of its own as soon as the result is longer than the string’s internal buffer: 15 characters with libstdc++ and 22 with libc++. std::format_to writes the same text through an output iterator, for example std::back_inserter into a string you clear and reuse, so a loop that formats a thousand rows into one reserved string allocates nothing. std::format_to_n writes at most n characters into a fixed buffer and returns where it stopped and how long the whole result is, so truncation can be detected; it suits stack buffers, C interfaces and code that may not use the heap. std::formatted_size gives the length first. All three take the same format strings as std::format, checked while compiling.

Two questions of this module’s bank start from this lesson: a logger whose formatting allocates under load, and a CSV export that wrote prices in exponent notation.

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Mid to senior

    A logging library builds every line with std::format and then writes it to a file. Under load, profiling shows memory allocation among the top costs. How do you remove the allocations without giving up checked format strings, and how do you prove that they are gone?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Fresher to mid

    A CSV export writes prices with std::format("{}", price). The finance team's importer rejects some rows, and you find values such as 1e+05 in the file. Explain why std::format wrote that, and what you would write for each column instead.

    Model answer, Pro. First step and follow-ups

Key takeaways

  • A format specification is [[fill]align][sign][#][0][width][.precision][L][type], every part optional and always in that order; 0 pads only when there is no alignment.
  • {} writes a double in its shortest exact form and picks exponent notation when it is shorter, so 100000.0 prints as 1e+05; give a type such as {:.2f} when the digits matter.
  • Widths count columns, estimated from grapheme clusters, not bytes; {:?} escapes text for logs, and C++23 formats ranges and maps.
  • std::format_to and std::format_to_n write into memory you already have; format_to_n reports the full size so you can detect a cut.
  • std::print and std::println take the same format strings and write to standard output, to a C FILE* such as stderr, or to any output stream.
  • Run-time format strings go through std::vformat and throw std::format_error; nested {} handles run-time widths without giving up the compile-time check.

Corrections

  • Paper P3505, which lets the size of a number choose between fixed and exponent notation, has been adopted for the standard after C++26; the section on doubles now says so.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.