Reputation: 388
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?
Upvotes: 16
Views: 13952
Reputation: 33864
Number 2 with amendment. (std::cout
and '\n'
)
Why?
using namespace std
. Sourcecout
is typesafe and printf
is not. Sourcestd::endl
will force a flush of the output buffer to the console. Unless you specifically want this to happen use << '\n'
or << "...string\n"
. SourceUpvotes: 23
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
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
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
Reputation: 168
Unless you really care about speed, both cout and printf are fine. If you want faster runtimes, here are a few pointers :
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.endl
unless you want to flush the output buffer. Lots of endl can make the code slower. Use cout<<'\n';
instead.Upvotes: 6