Take the 2-minute tour ×

This question already has an answer here:

There are obviously situations where, in C++ scanf() is preferable to cin, I was wondering if there are any situations where printf() is more practical than cout.

share|improve this question

marked as duplicate by GWW, J.N., Jules Mazur, maerics, Dave Rager Feb 7 '13 at 17:41


I can't provide a full answer, since I don't remember the details, but in a talk by a C++ standard committee member (he works for Facebook) he mentioned that he prefers printf() because cout doesn't work well with threads. –  Nikos C. Feb 7 '13 at 17:38
I'm curious as to what situations in C++ scanf() would be preferable to cin. –  Dave Rager Feb 7 '13 at 17:40
@DaveRager I find it easier to input and interpret certain strings using scanf(). Here's a line from my fist calculator program in C: scanf("%f%c%f", &num1, &op, &num2). op is a variable representing the mathematica operator (+, -, *, /), and, used properly, this can cut down on dozens of lines of useless code. –  Jules Mazur Feb 7 '13 at 17:47

2 Answers 2

up vote 3 down vote accepted

What you can do using ostream interface (so cout) is far superior to the old style printf(). First of all, it is type safe, so you won't get any segmentation violation when you mistakenly use a wrong formatting sequence.

An example. Imagine you have to print out attributes of the struct stat returned by Posix fstat function. The types of the attributes are defined using system dependant typedefs:

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    off_t     st_size;    /* total size, in bytes */
   /* ... more attributes */


So things like dev_t are different types (typedefs) on different systems. You may find that on your particular system dev_t is equivalent to, say, int, and write this:

printf("dev_t=%d", s.st_dev);

and it will work on your system, but when you compile it on another system, which defines, for example, dev_t not as int but, say, long, then your code will compile, but crash in runtime.

If you use C++ streams and overloaded << operator then stuff will always work correctly:

cout << "dev_t=" << s.st_dev;

Another important advantage of C++ streams is extensibility. It is not possible to extend the set of formatting sequences printf understands. In contrast, you can easily overload the << operator to conveniently print objects of your own types.

share|improve this answer

Short answer: Always!!! In c++ It makes sense to always use cout over printf because it gives you type safety unlike printf.

share|improve this answer
no doubt there are advantages associated with cout but the fact that cout is not thread safe, make printf superior in some aspect :), so especially in debugging if you are multi-threaded environment avoid cout etc... –  Saqlain Feb 7 '13 at 18:10
