The New std::print in C++23: A Modern Replacement for cout and printf
For decades, C++ developers have relied on two main ways to print output:
-
std::cout(from iostream) -
printf(from C)
But in C++23, we finally get something modern:
#include <print>
std::print("Hello, {}\n", name);
Yes — C++ now has a built-in, type-safe, Python-style printing function.
Let’s explore what std::print is, why it matters, and how it compares to older methods.
What Is std::print?
std::print is introduced in C++23 via the <print> header.
It is based on the formatting system from <format> (introduced in C++20), which itself was inspired by Python’s formatting style.
Example:
#include <print>
int main() {
std::string name = "Alice";
int age = 20;
std::print("Name: {}, Age: {}\n", name, age);
}
Output:
Name: Alice, Age: 20
Notice:
-
{}placeholders -
Automatic type safety
-
Clean and readable formatting
Comparing std::print with Older Methods
Let’s compare it side by side.
1️⃣ std::cout (Traditional C++ Way)
std::cout << "Name: " << name << ", Age: " << age << std::endl;
Pros
-
Type safe
-
Fully C++
-
Works everywhere
Cons
-
Verbose
-
Hard to format complex strings
-
Slower due to stream overhead
-
Chaining with
<<becomes messy
For example:
std::cout << std::setw(10) << std::setprecision(2) << value;
Formatting quickly becomes ugly.
2️⃣ printf (C Style)
printf("Name: %s, Age: %d\n", name.c_str(), age);
Pros
-
Fast
-
Concise formatting
-
Familiar to C developers
Cons
-
Not type safe
-
Easy to cause undefined behavior
-
Requires manual conversion (
c_str()) -
Format string errors are not caught at compile time
3️⃣ std::format (C++20)
Before std::print, we got:
#include <format>
std::string s = std::format("Name: {}, Age: {}", name, age);
std::cout << s << '\n';
Pros
-
Type safe
-
Modern formatting
-
Very clean syntax
Cons
-
Requires extra step (format → then output)
-
Slightly more verbose
4️⃣ std::print (C++23 Modern Way)
std::print("Name: {}, Age: {}\n", name, age);
Pros
-
Type safe
-
Concise
-
Modern formatting
-
No stream chaining
-
No C-style format specifiers
-
Cleaner and more readable
Cons
-
Requires C++23
-
Compiler support still evolving
-
Not yet universally available in all toolchains
MinGW on Windows doesn’t seem to support print yet! But clang and msvc works fine.
Performance Comparison (Conceptually)
In general:
-
printf→ Fast -
std::cout→ Slower (stream overhead) -
std::print→ Designed to be fast and modern
std::print avoids iostream’s heavy machinery and builds on the efficient formatting engine of <format>. For many use cases, it offers both safety and performance.
Why std::print Matters
std::print represents something bigger:
C++ is modernizing.
For years, developers complained that:
-
Printing was verbose
-
Formatting was awkward
-
C-style formatting was unsafe
-
Streams were slow and complicated
Now, we finally have a clean, safe, expressive solution built into the standard.
It brings C++ closer to languages like:
-
Python (
print(f"...")) -
Rust (
println!) -
JavaScript (
console.log())
Without sacrificing type safety.
When Should You Use std::print?
Use it if:
-
You’re compiling with C++23
-
Your compiler supports
<print> -
You want clean formatting
-
You want safer alternatives to
printf -
You dislike
<<chaining
If you’re stuck on older standards, std::format + std::cout is the closest modern alternative.
Final Thoughts
std::print is not just another function.
It’s the long-awaited modern printing solution for C++.
-
Cleaner than
std::cout -
Safer than
printf -
More direct than
std::format
If you’re learning modern C++, it’s worth adopting early.
C++23 finally makes printing elegant. And that’s something many of us have been waiting for.