Which is the best way to print to the console in c++?

  • A+
Category:Languages

I have read three ways to print things to the console in c++ from various sources.

  1. Using using namespace std; and then using cout (CodeBlocks Standard)
  2. Not using the above and using std::cout and std::endl; (C++ Primer)
  3. Using printf (HackerRank)

Which is preferred and why?

 


Number 2 with amendment. (std::cout and '/n')

Why?

  1. Because you should avoid using namespace std. Source
  2. (Among other reasons) Because cout is typesafe and printf is not. Source
  3. std::endl will force a flush of the output buffer to the console. Unless you specifically want this to happen use << '/n'. Source

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: