Quantcast
Channel: 'printf' vs. 'cout' in C++ - Stack Overflow
Viewing all articles
Browse latest Browse all 19

Answer by wohlstad for 'printf' vs. 'cout' in C++

$
0
0

Update 2023:

The idiomatic way to print to the console from c++23 is supposed to be with std::print, which uses {} placeholders in the format string syntax (see below):

#include <print>int main(){    std::print("{}, {} !", "Hello", "world");}

However as of now, the compilers support for it is very poor.

Meanwhile we can use the {fmt} library for a similar API.
Note that the c++ standard is actually based (mostly) on a subset of {fmt}.
See more about it here: What are the differences between libfmt and std::format?.
For more info about the format string syntax see here: {fmt} Format String Syntax.

Working example:

#include <fmt/core.h>int main(){    fmt::print("{}, {} !", "Hello", "world");}

Live demo - Godbolt


Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>