C++ Reference ยท Strings and I/O
C++ std::print and std::println functions
Reference for std::print and std::println: print formatted text to stdout, a FILE* or an ostream, add the newline for you, and know how they differ from cout.
std::print and std::println (C++23, header <print>) format their arguments with the same rules as std::format and write the result straight to an output. println is print plus a trailing newline. By default the output goes to stdout, but there are overloads for a std::FILE* and for any std::ostream.
They replace most uses of std::cout << a << b << '\n' and printf. The format string is checked at compile time, nothing is sticky, and the whole line is produced by one call, which also keeps text from different threads from interleaving mid-message.
#include <print> int main() { std::println("Hello, {}! You are {} years old.", "Ada", 36); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class... Args> void print(std::format_string<Args...> fmt, Args&&... args); template<class... Args> void print(std::FILE* stream, std::format_string<Args...> fmt, Args&&... args); template<class... Args> void print(std::ostream& os, std::format_string<Args...> fmt, Args&&... args); template<class... Args> void println(std::format_string<Args...> fmt, Args&&... args); template<class... Args> void println(std::FILE* stream, std::format_string<Args...> fmt, Args&&... args); template<class... Args> void println(std::ostream& os, std::format_string<Args...> fmt, Args&&... args); void println(); // newline only void println(std::FILE* stream); void println(std::ostream& os);
| Name | Description |
|---|---|
stream |
A C stream such as stdout or stderr, or a file opened with std::fopen. |
os |
Any std::ostream: std::cout, std::cerr, a file stream or a string stream. |
fmt |
The format string, checked at compile time. See format specifications. |
args |
The values to print. |
Returns: nothing (void).
Errors / undefined behavior: a bad format string is a compile-time error. The std::FILE* overloads throw std::system_error if writing to the stream fails. A null FILE* is undefined behavior. Formatting a user type whose formatter throws propagates that exception, and nothing is written for that call.
Usage notes
- Added in C++23. The zero-argument
println()overloads were added to C++23 later by a defect report; GCC 14 and later accept them. - Unicode. When the output is a terminal,
printwrites UTF-8 text in a way that works correctly on Windows, whereprintfandcoutare subject to the console code page. For redirected output it writes the bytes unchanged. - Mixing with
coutis safe whenstd::ios::sync_with_stdio(false)has not been called. The ostream overloads write through the stream, so they always stay in order with<<output. - No implicit flush. Like
printf,printdoes not flush. Output to a terminal is usually line buffered; callstd::fflush(stdout)or usestd::cout << std::flushwhen you need it at a precise moment. - Errors go to
stderr:std::println(stderr, "error: {}", msg);. - Not a replacement for input. There is no
std::scanin C++23 (it is proposed for C++26); keep usingcin,getlineorscanffor reading. - Header cost.
<print>pulls in<format>and is a heavier include than<cstdio>, which shows up in compile times for tiny programs.
Related
std::format, Format specifications,std::cout,std::cin- Lessons: Output, String formatting