Skip to content

Guide · Open source

How to use fmt: C++ formatting library guide

Updated · written with AI from the project's own documentation and checked against our catalogue data

fmt is an open-source C++ library for turning values into formatted text, whether you print to the screen, write to a file, or build a string. This guide is for C++ developers who want a safer alternative to printf and iostreams. By the end you will know where to get the source, how to print and format values, and what to check before using it in a project.

{fmt}fmtlib/fmt · ★ 25.9k · MIT

Quick start

  1. 1Clone the repository. Run the git clone command to download the fmt source from GitHub.
  2. 2Check prerequisites. Confirm you have a C++ compiler with C++11 or later support. Check the docs for CMake and build details.
  3. 3Include the header. Add #include <fmt/core.h> to the source file where you want to print or format text.
  4. 4Call fmt::print or fmt::format. Use fmt::print to write to standard output or fmt::format to produce a std::string.
  5. 5Add placeholders and arguments. Write {} for each value, or {0}, {1} for positional arguments.
  6. 6Add optional headers. Include fmt/ranges.h for containers or fmt/chrono.h for dates and times when you need them.

What fmt is and when to use it

The README describes {fmt} as a fast and safe alternative to C stdio and C++ iostreams. It uses a Python-like format string syntax and implements C++20 std::format and C++23 std::print.

  • Good fit: C++ code that needs a printing or string formatting API safer than printf
  • Good fit: programs that convert many numbers to text
  • Good fit: teams that want consistent output across Linux, macOS and Windows
  • Good fit: types that need a custom formatter
  • Not a fit: projects that do not write C++
  • Not a fit: projects that already use std::format or std::print and need nothing extra

Get the source code and build it

The install method is clone-and-build. The README's visible install command clones the repository. The README excerpt provided does not spell out the full build steps, the minimum compiler version beyond C++11 support, or the CMake requirement, so check the documentation at fmt.dev for those details.

  • Requires a C++ compiler with C++11 or later support, according to the analysis
  • CMake is listed as a prerequisite, but the visible README does not confirm this
git clone https://github.com/fmtlib/fmt.git

The simplest use is fmt::print, which writes formatted text to standard output. Include fmt/core.h and call it like a function with a format string and arguments. Placeholders are written as {}.

#include <fmt/core.h> int main() { fmt::print("Hello, world!\n"); }

Build formatted strings with fmt::format

Use fmt::format when you need the result as a std::string instead of printed output. Positional arguments let you reorder values by index, which the README notes is useful for localization.

  • Replace each {} with the next argument
  • Use {0}, {1} to pick arguments by position
std::string s = fmt::format("The answer is {}.", 42); // s == "The answer is 42." std::string s2 = fmt::format("I'd rather be {1} than {0}.", "right", "happy"); // s2 == "I'd rather be happy than right."

Catch format string errors at compile time

The README says errors in format strings can be reported at compile time. For example, using the d specifier on a string is rejected by the compiler in C++20.

std::string s = fmt::format("{:d}", "I am not a number");

Format containers, dates and custom types

Including fmt/ranges.h lets you print containers such as std::vector. Including fmt/chrono.h enables date and time output. The README also documents formatting user-defined types through a custom formatter, with details in the API docs at fmt.dev.

  • fmt/ranges.h: print containers
  • fmt/chrono.h: print dates and times, with specifiers such as {:%H:%M}
  • Custom formatters: see the formatting user-defined types section of the docs
#include <vector> #include <fmt/ranges.h> int main() { std::vector<int> v = {1, 2, 3}; fmt::print("{}\n", v); }

What people use it for

  • Printing formatted diagnostics to the terminal in a C++ application
  • Building formatted strings for logs or messages
  • Converting large numbers of values to text quickly
  • Writing formatted text to a file with fmt::output_file
  • Printing coloured or styled terminal output with fmt/color.h

Frequently asked questions

Is fmt free to use?

Yes. The project is licensed under MIT, a permissive licence. The README also describes the library as having no external dependencies.

How do I install fmt?

The README's install method is cloning the repository with git clone https://github.com/fmtlib/fmt.git and building it. The visible README does not give full build steps, so see the docs at fmt.dev.

What do I need to use fmt?

You need a C++ compiler with C++11 or later support. CMake is listed as a prerequisite in the analysis, but the visible README does not confirm it.

Is fmt the same as std::format?

No. fmt implements C++20 std::format and C++23 std::print, and the README says so. If your project already uses those standard functions and needs nothing more, the standard library may be enough.

What are the alternatives to fmt?

The comparable projects listed are std::format, iostreams, Boost.Format and spdlog. Each serves a different need, so compare them against your project's requirements.

Why does my format string fail to compile?

fmt checks format strings at compile time in C++20. For example, the d specifier is invalid for a string, and the compiler reports that error instead of failing at runtime.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

fmt suits C++ developers who want a safer, fast printing and formatting API that works across Linux, macOS and Windows. If you do not write C++, or you already rely on std::format or std::print with no extra needs, you may not need it.

Install options & downloads