1

Many of us learned using printf() long before we learn to use constructors and destructors. So when it's time to switch to C++ many will stick to printf() for console output.

Sometimes you can hear:

printf() is bad, you should use cout << instead, because it's C++

What is an advantage of abandoning using printf() and switching to cout <<?

exebook
  • 32,014
  • 33
  • 141
  • 226
  • 1
    @SLaks: decent compilers issue a diagnostic even when you make type errors with `printf`. – 6502 Jan 15 '14 at 18:02

2 Answers2

6

C++ faq states that:

Why should I use <iostream> instead of the traditional <cstdio>?

Increase type safety, reduce errors, allow extensibility, and provide inheritability. printf() is arguably not broken, and scanf() is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (using << and >>) is, relative to C (using printf() and scanf()).

  • More type-safe: With <iostream>, the type of object being I/O'd is known statically by the compiler. In contrast, uses "%" fields to figure out the types dynamically.
  • Less error prone: With <iostream>, there are no redundant "%" tokens that have to be consistent with the actual objects being I/O'd. Removing redundancy removes a class of errors.
  • Extensible: The C++ <iostream> mechanism allows new user-defined types to be I/O'd without breaking existing code. Imagine the chaos if everyone was simultaneously adding new incompatible "%" fields to printf() and scanf()?!
  • Inheritable: The C++ <iostream> mechanism is built from real classes such as std::ostream and std::istream. Unlike <cstdio>'s FILE*, these are real classes and hence inheritable. This means you can have other user-defined things that look and act like streams, yet that do whatever strange and wonderful things you want. You automatically get to use the zillions of lines of I/O code written by users you don't even know, and they don't need to know about your "extended stream" class.
haccks
  • 104,019
  • 25
  • 176
  • 264
4

cout is more object-oriented and provides some advantages. But I'm an old-school developer and very comfortable with printf(). Although, I don't write many console apps these days, I am likely to use printf() if I do.

That's one nice thing about it: You can choose which one you like best.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    And, printf() is *way* more readable than an endless sequence of `>>` operators in most situations. Usually, you can look at the printf() format string and see the form the output will take. In fact, you can view the printf() format string as a "domain-specific language" that matches the problem domain of formatted output. In that sense, C++ I/O is a gigantic step backward -- you can't glance at all those `>>` operators and quickly grok what the output will look like. C++ I/O is a failed experiment, IMHO. To like it, you have to focus on type safety and ignore all you're giving up... – Ron Burk Jan 15 '14 at 18:48