Why use std::print when C++ already has std::cout?

November 21, 2026 4 answers
Asked by 3h~cobblepiston_648

C++23 adds std::print, but std::cout already lets me print values with type checking. Is the main benefit just syntax?

For a small Hello World program, g++ -O2 -S produced roughly 30 lines of assembly with std::cout and 29,000 with std::print. What could explain such a big difference?

Answers

Tavrixa-8q

Syntax is a big part of it, but formatting is the more useful distinction. std::print uses format strings, so the text and its placeholders stay together, and you can specify things like width, precision, or hexadecimal output for individual values.

With std::cout, formatting manipulators can change stream state. For example, switching to hexadecimal can affect later output until you change it back. A format specification keeps that choice local to the formatted value.

Both approaches check argument types, but std::print also checks ordinary compile-time format strings against their arguments. That does not make either approach a replacement for every use of the other.

3h~cobblepiston_648:

Keeping the formatting local makes sense. I was mostly thinking of simple text output, not a longer sequence with different number formats.

Reply to this answer
7v+brindlevisor_394

Assembly line count is a pretty rough comparison here. The compiler may emit formatting implementation code for std::print while the std::cout version mostly contains calls to code supplied by a library. That depends on the compiler, standard library, and build setup, so the listing alone does not show how much code runs or prove that one executable is bigger.

Compare the final linked binaries with the same options, and check whether each uses shared or static libraries. If runtime speed matters, benchmark a representative workload too. A Hello World listing won't tell you much about that.

_c87~mivdaro.4:

And a larger listing doesn't necessarily mean all of that code executes for the one print call.

Reply to this answer
q.438!plendaro

For teaching, std::print can be a nicer starting point. You can show a format string and some values without first explaining streams and why the left shift operator is used for output.

That doesn't mean std::cout is obsolete. If the surrounding code already works with streams, sticking with them can be the simpler choice.

Reply to this answer
2e!claspnotch_738

I'm not convinced the newer interface is automatically the right choice for released code. Readability is useful, but generated size and performance may matter for a particular project. Check those in the actual build rather than assuming either interface wins.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions