I generally use cout
and cerr
to write text to the console. However sometimes I find it easier to use the good old printf
statement. I use it when I need to format the output.
One example of where I would use this is:
// Lets assume that I'm printing coordinates...
printf("(%d,%d)\n", x, y);
// To do the same thing as above using cout....
cout << "(" << x << "," << y << ")" << endl;
I know I can format output using cout
but I already know how to use the printf
. Is there any reason I shouldn't use the printf
statement?
No reason at all. I think it's just some strange ideology that drives people towards using only C++ libraries even though good old C libs are still valid. I'm a C++ guy and I use C functions a lot too. Never had any problems with them.
Use printf. Do not use C++ streams. printf gives you much better control (such as float precision etc.). The code is also usually shorter and more readable.
Google C++ style guide agrees.
My students, who learn
cin
andcout
first, then learnprintf
later, overwhelmingly preferprintf
(or more usuallyfprintf
). I myself have found theprintf
model sufficiently readable that I have ported it to other programming languages. So has Olivier Danvy, who has even made it type-safe.Provided you have a compiler that is capable of type-checking calls to
printf
, I see no reason not to usefprintf
and friends in C++.Disclaimer: I am a terrible C++ programmer.
I often "drop back" to using
printf()
, but more oftensnprintf()
for easier formatted output. When programming in C++ I use this wrapper I wrote a while back, called like this (to use your example as above):cout << format("(%d,%d)\n", x, y);
Here's the header (
stdiomm.h
):And the source (
stdiomm.cpp
):Update
After reading some of the other answers, I might have to make a switch to
boost::format()
myself!I almost always use printf for temporary debugging statements. For more permanent code, I prefer the 'c' streams as they are The C++ Way. Although boost::format looks promising and might replace my stream usage (especially for complexly formatted output), probably nothing will replace printf for me for a long time.
Adaptability
Any attempt to
printf
a non-POD results in undefined behaviour:The above printf-calls yield undefined behaviour. Your compiler may warn you indeed, but those warnings are not required by the standards and not possible for format strings only known at runtime.
IO-Streams:
Judge yourself.
Extensibility
C:
or:
Note how you have to take care of using the proper call arguments/signatures in C (e.g.
person_fprint(stderr, ...
,person_fprint(myfile, ...
), where in C++, the "FILE
-argument" is automatically "derived" from the expression. A more exact equivalent of this derivation is actually more like this:I18N
We reuse our Person definition:Judge yourself.
I find this less relevant as of today (2017). Maybe just a gut feeling, but I18N is not something that is done on a daily basis by your average C or C++ programmer. Plus, it's a pain in the a...natomy anyways.
Performance
If you use iostreams consistently, you can
and reap equal runtime with a good compiler:
Results (
g++ -O3 synced-unsynced-printf.cc
,./a.out > /dev/null
,cat RESULTS
):Judge ... yourself.
No. You won't forbid me my printf.
You can haz a typesafe, I18N friendly printf in C++11, thanks to variadic templates. And you will be able to have them very, very performant using user-defined literals, i.e. it will be possible to write a fully static incarnation.
I have a proof of concept. Back then, support for C++11 was not as mature as it is now, but you get an idea.
Temporal Adaptability
Later, your data grows so big you must do
It is an interesting exercise maintaining that and doing it bug-free. Especially when other, non-coupled projects use foo.h.
Other.
Bug Potential: There's a lot of space to commit mistakes with printf, especially when you throw user input bases strings in the mix (think of your I18N team). You must take care to properly escape every such format string, you must be sure to pass the right arguments, etc. etc..
IO-Streams make my binary bigger: If this is a more important issue than maintainability, code-quality, reuseability, then (after verifying the issue!) use printf.