format/formatchars.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <string>
#include <print>

int main()
{
  std::string s = "value: '\t'";    // some chars with single quote and tabulator
  std::println("default:     {}", s);
  std::println("{{:s}} :       {:s}", s);
  std::println("{{:?}} :       {:?}", s);
  std::println("{{:.3}} :      {:.3}", s);
  std::println("{{:.3?}} :     {:.3?}", s);
  std::println("{{:*^20?}} :   {:*^20?}", s);
  std::println("{{:*^20.3?}} : {:*^20.3?}\n", s);

  std::println("s[7]:");
  std::println("  {{}} :       {}", s[7]);
  std::println("  {{:?}} :     {:?}", s[7]);
}