正常
std::print (C++23)
TL;DR
Writes formatted strings straight to stdout — std::format and std::cout rolled into one, the new way to write Hello World in C++23.
Header
#include <print>
Core API Cheat Sheet
| Operation | Signature | Description |
|---|---|---|
| Output to stdout | void print(format_string, args...) | Formats and writes to standard output |
| Output with newline | void println(format_string, args...) | Automatically appends a newline |
| Blank line | void println() | Outputs just a newline character |
| Output to file | void print(FILE* f, format_string, args...) | Writes to a given C file stream |
| Output to file with newline | void println(FILE* f, format_string, args...) | Newline version |
| Output to stream | void vprint_unicode(std::ostream&, std::format_args) | Writes to a C++ stream |
Minimal Example
C++
// Standard: C++23
#include <print>
int main() {
std::print("Hello, {}!\n", "world");
std::println("value = {}", 42);
std::println("{:>10.2f}", 3.14159); // 3.14
std::println(); // blank line
}Embedded Applicability: Low
- Depends on
stdoutand the filesystem abstraction layer; bare-metal environments usually have no standard output - A good fit for log output in embedded Linux host-side tools and test frameworks
- The formatting engine carries significant Flash overhead; not recommended for extremely resource-constrained devices
- You can fall back to
fmt::printfrom the{fmt}library, available since C++11
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 14 | 18 | 19.34 |
See Also
Some content is adapted from cppreference.com, used under the CC-BY-SA 4.0 license