I have read three ways to print things to the console in c++ from various sources.
using namespace std;
and then using cout
(CodeBlocks Standard)std::cout
and std::endl;
(C++ Primer)printf
(HackerRank)Which is preferred and why?
A modern console consists of the keyboard and a window on a computer screen. cin (console in), cout (console out), and cerr (console error) are stream objects that become part of every C++ program. The console objects channel streams of bytes to and from running programs.
To print a String to console output, you can use System. out. print() or System.
The printf function formats and prints a series of characters and values to the standard output stream, stdout .
Number 2 with amendment. (std::cout
and '\n'
)
Why?
using namespace std
. Source
cout
is typesafe and printf
is not. Source
std::endl
will force a flush of the output buffer to the console. Unless you specifically want this to happen use << '\n'
or << "...string\n"
. Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With