C++ by Example
C++ by Example

String Formatting

2 min read

String Formatting

C++ has several ways to format text into strings. The right choice depends on which C++ version you are targeting.

std::format (C++20)

std::format is the modern answer, modeled on Python's str.format and Rust's format!. It is type-safe, composable, and readable:

#include <format>
#include <iostream>
#include <string>

int main() {
    std::string msg = std::format("Hello, {}!", "World");
    std::cout << msg << "\n";

    // Numbers
    double pi = 3.14159265;
    std::cout << std::format("{:.4f}\n", pi);       // 3.1416
    std::cout << std::format("{:10.2f}\n", pi);     // "      3.14" (width 10)
    std::cout << std::format("{:e}\n", pi);          // 3.141593e+00

    // Integers
    int n = 255;
    std::cout << std::format("{:d}\n",  n);   // 255 (decimal)
    std::cout << std::format("{:x}\n",  n);   // ff  (hex)
    std::cout << std::format("{:o}\n",  n);   // 377 (octal)
    std::cout << std::format("{:b}\n",  n);   // 11111111 (binary)

    // Padding and alignment
    std::cout << std::format("{:>10}\n",  "right");   // "     right"
    std::cout << std::format("{:<10}\n",  "left");    // "left      "
    std::cout << std::format("{:^10}\n",  "center");  // "  center  "
    std::cout << std::format("{:*^10}\n", "hi");      // "****hi****"
}

std::println (C++23) combines std::format with output and a trailing newline:

std::println("Value: {}", 42);

std::ostringstream

For C++17 and earlier, build formatted strings with a string stream:

#include <sstream>
#include <iomanip>
#include <string>
#include <iostream>

int main() {
    std::ostringstream oss;
    oss << "x = " << 42 << ", pi ≈ " << std::fixed << std::setprecision(3) << 3.14159;
    std::string result = oss.str();
    std::cout << result << "\n";

    // Hex
    std::ostringstream hex;
    hex << "0x" << std::hex << std::uppercase << 255;
    std::cout << hex.str() << "\n";   // 0xFF
}

std::ostringstream is verbose but widely supported. The <iomanip> manipulators control width, precision, alignment, and base.

printf (C-style)

printf is inherited from C. It is fast and terse, but not type-safe — passing a wrong type is undefined behavior:

#include <cstdio>

int main() {
    printf("Hello, %s!\n", "World");
    printf("Pi: %.4f\n", 3.14159);
    printf("Hex: %x\n", 255);       // ff
}

Common format specifiers:

Specifier Type
%d int
%u unsigned int
%f double (fixed)
%e double (scientific)
%s const char*
%c char
%x int as hex

printf is still common in performance-critical code and when interfacing with C libraries. Do not use it in new C++ code when std::format is available.

snprintf for safe C-style formatting

When you need a C-style formatted string in a buffer:

char buf[64];
std::snprintf(buf, sizeof(buf), "Value: %d", 42);
std::string s = buf;

snprintf (unlike sprintf) takes a maximum buffer size, preventing overflow.

C++ by Example
C++ by Example

Learn modern C++ through working code. Each chapter introduces one concept — variables, functions, classes, templates, smart pointers, concurrency — with a clear example, a line-by-line explanation, and notes on how it applies in real programs.

View book →