1

I have recently started learning C++ and I know a bit of C. I was wondering why people mostly use the cout statement instead of the printf() function in C++ (if they even do, I had just never seen use the printf function in C++). Based on my limited experience, I have found that I prefer the printf function since I find that it gives me much more control over what I'm printing out. Could someone explain the difference and which one you prefer?

Serket
  • 3,785
  • 3
  • 14
  • 45
  • 1
    You can overload `operator<<` to support your own class with `cout` (and other `std::ostream` classes) - you don't have any such option for `printf` – UnholySheep Apr 28 '20 at 22:08
  • 3
    `printf` is not type-safe. This is why most people prefer `cout`. – Aykhan Hagverdili Apr 28 '20 at 22:15
  • "From my limited experience, I have found that I prefer the printf function since I find that it gives me much more control over what I'm printing out." - As you said yourself this is only based on your limited experience. I don't see why C++ I/O streams give less control than C I/O functions. – eesiraed Apr 28 '20 at 22:17
  • 2
    Consider using [{fmt}](https://fmt.dev/latest/) instead of the unsafe functions defined in ``. Also note that you can control input/output streams with [manipulators](https://en.cppreference.com/w/cpp/io/manip). – Bob__ Apr 28 '20 at 22:20
  • I would doubt the premise that "people mostly use cout". In beginner tutorials with console input and output, perhaps yes, but that's mostly because they do not bother with the dirty details of error handling and stream status. Lat not least `` is huge and leads to long compile times. – Peter - Reinstate Monica Apr 28 '20 at 22:31

3 Answers3

1

cout is the C++ version of printf. printf requires you to specify the type, cout does not. printf is generally faster as well. So cout is not so fast but easy, and printf is fast, but not way to easy if you are not familiar with it. When using c++, cout should be used, unless you need printf. I prefer cout because it is type-safe (big thing!), and it is more c++ish (if that makes sense).

Notes:

  • printf is in stdio
  • printf came from c
  • printf is not type safe!
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
xilpex
  • 3,097
  • 2
  • 14
  • 45
1

This answer 'printf' vs. 'cout' in C++ explains well the differences between std::cout and printf. Both have their own advantages and shortcomings.

iostreams offers additional features such as type safety and extensibility.

Bugman
  • 191
  • 6
0

I think it's because you need to add specific parameters

printf ("Characters: %c %c", 'a', 65);

%c - char , %d - integer, %f - float

You need to add for example %d to declare that variable type is integer

In cout you don't need to do this, you can even add strings or more variables

cout << "Hello\n";
int a = 7;
cout << "Number seven is : " << a;

This one will print

Hello
Number seven is : 7

I think that most of programmers prefers cout

Demonxus
  • 64
  • 7
  • 1
    Your second example should be `cout << "Number seven is : " << a;` – UnholySheep Apr 28 '20 at 22:14
  • I find that the parameters give me a higher level of control, which is (in my opinion) the purpose of a low-level programming language. To me, it seems like cout belongs in a language like Python or javascript instead of C++ – Serket Apr 28 '20 at 22:14
  • @UnholySheep That's right. I'm sorry I currently started programming in Java so I made a big mistake. I edited my answer. – Demonxus Apr 28 '20 at 22:29