1

Possible Duplicate:
printf vs cout in C++

What are the differences between cout and printf?

Community
  • 1
  • 1
lital maatuk
  • 5,921
  • 20
  • 57
  • 79

4 Answers4

1

cout automatically make casts and finds out the type of the variables you are trying to print. So you can do something like:

int myint = 5;
cout << myint;

And cout will detect that myint is an int and print it. With printf, you have to specify what is the type of the variable you are trying to print:

int myint = 5;
printf("%d", myint);

Also, cout is slower than printf (because it does the type detection...), although in most practical applications, you won't notice the performance difference.

Piva
  • 982
  • 6
  • 13
  • 1
    That "type detection" for cout happens at compile time, however parsing the format string of printf to find out which types are printed happens at runtime, so if you only consider the "type" aspect, cout should be faster than printf. – smerlin Feb 12 '11 at 14:22
  • 1
    Type detection is completely solved at compile-time, so it should be a speed bonus over `printf`; the speed problems come from the rest of the infrastructure that's under the hood in the iostream library. – Matteo Italia Feb 12 '11 at 14:23
1

printf is the function used for printing data on the standard output of the stdio library, the IO library of C. It's kept in C++ mainly for legacy reasons, although sometimes it's still useful.

cout is a C++ stream from the iostreams library (in particular, it's defined to be a ostream &); the iostreams library is the native C++ way to perform IO.

In general it's easier and safer to use iostreams than the old printf-like functions (thanks to << operator overloading instead of format strings+varargs), and it's the C++ "idiomatic" way to perform IO, so you should use it unless you have specific needs not to do so.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

Basically, cout is the C++ way of outputting to standard output while printf is the C way.

C++ iostreams (of which cout is one) are based on C++ classes and are extensible to handle new classes. In other words, you can create a class called foo and then do:

foo bar;
std::cout << bar << std::endl;

On the other hand, printf cannot handle new types, you have to write functions which call printf for each of the components of that type, where each component is already a type known to printf (such as int or char *).

There's really no excuse for using printf in C++ code. I always say that, if you're going to use C++, you should use it, not wallow in the old world :-) If you want to use printf, stick with C.


If you're looking for an example of how to allow your class to be used in iostreams, see an answer I provided to an earlier question regarding the code that does it.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • How can I print foo instances using cout in you example? Do I have to overload the << symbol for the foo class? – lital maatuk Feb 12 '11 at 14:22
  • @lital maatuk: yes, you have to create an `operator<<` overload; see the FAQ about operator overloading (http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719). – Matteo Italia Feb 12 '11 at 14:26
  • @lital, I added a link to another answer of mine, showing how it's done. – paxdiablo Feb 12 '11 at 14:36
0

taken from http://forums.devshed.com/c-programming-42/difference-between-cout-and-printf-38373.html

cout is an object of the iostream in C++. If you are using C++, then use cout, it works well. printf while doing some the same things, it is a formatting function that prints to the standard out. This is mainly used in C.

so printf is the big brother of cout in a way, as it allows you to format strings.

Augusto
  • 28,839
  • 5
  • 58
  • 88