C++ std::print() Function (C++23)

Beginner
5 min read
Updated: Nov 2026
3 examples
C++23

Definition

In C++23, std::print() writes text to the terminal. It uses a format string with {} placeholders, just like Python's print and C++20 std::format.

The values you pass fill the placeholders in order. Unlike cout you do not chain <<, and print adds no new line. Include <print> and compile with -std=c++23.

This is a C++23 function. It is not a stream member: it writes text directly to the terminal (or to a FILE*) without using std::cout. Compile with a C++23 setting such as -std=c++23.

Syntax

Give a format string with {} placeholders, followed by the values. This is a C++23 feature.

C++
#include <print>

std::print("Hello, {}!\n", "World");
std::print("{} + {} = {}\n", 2, 3, 2 + 3);
std::print(stderr, "error: {}\n", code);   // to a FILE*
CallWhat it does
std::print("Hi {}", name)Prints Hi and the value of name, with no new line.
std::print("{:>6}", 42)Right-aligns 42 in a field of six places.
std::print(stderr, "oops")Prints to the standard error stream.

Use std::println when you want a new line at the end. Check that your compiler supports <print>. Older ones do not.

Library declarations
C++
template<class... Args>
void print(std::format_string<Args...> fmt, Args&&... args);

template<class... Args>
void print(FILE* stream, std::format_string<Args...> fmt, Args&&... args);

template<class... Args>
void print(std::ostream& os, std::format_string<Args...> fmt, Args&&... args);

Parameters

ParameterDescription
stream / osOptional. A FILE* such as stdout or stderr, or an std::ostream. Without it, the text goes to stdout.
fmtRequired. The format string. Each {} is replaced by the next value, and {:spec} adds formatting.
argsThe values to print. Their number must match the placeholders.

Return Value

Return ValueDescription
Nothingprint() returns void. A wrong format string is a compile-time error.

C++ Examples

Example 1 — Print Text with a Placeholder

The value "World" replaces the placeholder. Compile with -std=c++23.

C++
#include <print>

int main() {
    std::print("Hello, {}!\n", "World");
    return 0;
}

How It Works

  1. The format string is "Hello, {}!\n". The pair {} is a placeholder.
  2. "World" is the first value, so it goes into the first placeholder.
  3. The \n at the end of the format string makes the new line, because print() adds none.

Example 2 — Width and Decimals Inside the Braces

After a colon, a placeholder can set alignment, width, and decimals.

C++
#include <print>

int main() {
    std::print("[{:>6}]\n", 42);
    std::print("[{:<6}]\n", 42);
    std::print("{:.2f}\n", 3.14159);
    return 0;
}

How It Works

  1. {:>6} means: right-aligned in a field of six places. {:<6} means left-aligned.
  2. The two lines show the same number padded on different sides. Brackets make the field visible.
  3. {:.2f} prints a floating-point number with two digits after the point, which is like fixed with setprecision(2).

Example 3 — Hex and Binary Without Manipulators

The format letters x and b replace hex and a bitset.

C++
#include <print>

int main() {
    std::print("{:x} {:#x} {:b}\n", 255, 255, 5);
    return 0;
}

How It Works

  1. {:x} prints 255 in hex, which is ff.
  2. {:#x} adds the 0x prefix, like showbase. {:b} prints 5 in binary: 101.
  3. The settings belong to one placeholder only, so nothing stays on afterwards. This is different from cout << hex.

Frequently Asked Questions

A C++23 function that prints formatted text using a format string with {} placeholders. It needs #include <print>.
print uses a format string and placeholders. cout chains values with <<. print settings affect only one placeholder, while cout manipulators stay on.
No. Add \n to the format string, or use std::println.
You need a compiler and library with C++23 support, such as GCC 14 or later, and the option -std=c++23.
Pass the stream first: std::print(stderr, "error: {}\n", code).

Next: println()

Print formatted text followed by a new line (C++23).

println() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful