Devesh Lohumi
Devesh Lohumi

Reputation: 388

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

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?

Upvotes: 16

Views: 13952

Answers (5)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33864

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' or << "...string\n". Source

Upvotes: 23

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16017

The answer depends a lot on what you want to do. For output which largely uses default formats cout is indeed preferred because of the type safety and because it's very intuitive.

If you want to heavily format your output though I can only recommend the surprisingly versatile and straight-forward printf because manipulators in cout are a pain. True: The printf format syntax, does, let's say, take some getting used to, but it's surely worth it. Just double check the format string, listen to the warnings of your compiler, and use the proper format specifiers e.g. for size_t and other system dependent data in order to stay portable.

There is also a boost facility for combining streams and printf style formatting, see https://stackoverflow.com/a/15106194/3150802, but I have never used it. Perhaps somebody can comment on its usability?

Upvotes: 1

Alex Meuer
Alex Meuer

Reputation: 1779

Both your first points do basically the same thing. It's better practice to use std:: instead of using namespace std; as the latter pollutes the global namespace and can cause naming conflicts.

Something not mentioned is that you can selectively expose parts of a namespace with using <namespace>::<element>; (e.g. using std::cout;). It's still better practice to be verbose with your statements, but this option still isn't as bad as exposing the entire namespace.

printf isn't as safe as cout (the stream << operators do a good job of printing what you want), you ought to avoid it while starting out.

Upvotes: 1

Ehsan Panahi
Ehsan Panahi

Reputation: 130

these is my debugger code that during these 10 years of c++ working helped me.

std::ostream &debugRecord (const char* fileName, int lineNum, const char* funcName)
{
    std::lock_guard<std::mutex> lock(streamMutex_);
    return std::cout << "Thread # " << getCurrentThreadId() << " -- "
                     << "(" << fileName << ":" << lineNum << "): " << funcName << std::endl;
}

Upvotes: 1

Ashutosh Kumar Verma
Ashutosh Kumar Verma

Reputation: 168

Unless you really care about speed, both cout and printf are fine. If you want faster runtimes, here are a few pointers :

  • Use only printf with no cout. This will give more speed than using a mixture of printf and cout or just cout.
  • Or use only cout but add the following at the beginning of execution ios_base::sync_with_stdio(false);cin.tie(NULL); . There are two separate streams for printf and cout and they are synchronized by default. Lot of running time is wasted due to this synchronisation. These two lines of code will stop the synchronisation, but take care that you don't use any printf if you add these lines, otherwise printing might happen in random order.
  • Do not use endl unless you want to flush the output buffer. Lots of endl can make the code slower. Use cout<<'\n'; instead.

Upvotes: 6

Related Questions