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?
My students, who learn
cinandcoutfirst, then learnprintflater, overwhelmingly preferprintf(or more usuallyfprintf). I myself have found theprintfmodel 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 usefprintfand friends in C++.Disclaimer: I am a terrible C++ programmer.