Home C++ Iostream Functions print() C++ std::print() Function (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.
#include <print>
std::print("Hello, {}!\n", "World");
std::print("{} + {} = {}\n", 2, 3, 2 + 3);
std::print(stderr, "error: {}\n", code); // to a FILE* Call What 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 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 Parameter Description 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 Value Description Nothing print() returns void. A wrong format string is a compile-time error.
Important
print() does not add a new line, so end the format string with \n or use println(). It needs a compiler with C++23 library support: GCC 14, Clang 18 with a matching library, or a recent Visual Studio. To print a literal brace, double it in the format string.
C++ Examples 1. Hello 2. Format specifiers 3. Number bases Example 1 — Print Text with a Placeholder The value "World" replaces the placeholder. Compile with -std=c++23.
#include <print>
int main() {
std::print("Hello, {}!\n", "World");
return 0;
} How It Works The format string is "Hello, {}!\n". The pair {} is a placeholder. "World" is the first value, so it goes into the first placeholder.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.
#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 {:>6} means: right-aligned in a field of six places. {:<6} means left-aligned.The two lines show the same number padded on different sides. Brackets make the field visible. {:.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.
#include <print>
int main() {
std::print("{:x} {:#x} {:b}\n", 255, 255, 5);
return 0;
} How It Works {:x} prints 255 in hex, which is ff.{:#x} adds the 0x prefix, like showbase. {:b} prints 5 in binary: 101.The settings belong to one placeholder only, so nothing stays on afterwards. This is different from cout << hex. Frequently Asked Questions What is std::print in C++? A C++23 function that prints formatted text using a format string with {} placeholders. It needs #include <print>.
What is the difference between std::print and std::cout? print uses a format string and placeholders. cout chains values with <<. print settings affect only one placeholder, while cout manipulators stay on.
Does std::print add a new line? No. Add \n to the format string, or use std::println.
Why can I not compile std::print? You need a compiler and library with C++23 support, such as GCC 14 or later, and the option -std=c++23.
How do I print to stderr with print? Pass the stream first: std::print(stderr, "error: {}\n", code).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion