puts vs cout vs printf

Hello!

When watching different tutorial videos on C++, some teachers still use C keywords printf and put instead of cout with C++. I am just wondering in which situation do we have to used each keywords.

I read in stackoverflow that printf executes faster and more typesafe (due to format specifiers) than cout. However, cout can be useful for overloading.
Can you please link the SO page? The issue with printf is quite the opposite; it isn't type-safe and requires parsing of strings for types at runtime. <iostream> objects can cause a bigger executable size but performance can be on par with C I/O.

The reason you'll see that used is that the teacher probably has a C background and is more comfortable with its syntax than C++ streams, or thinks that concatenation of << operators is needlessly verbose. It isn't really a big deal imo.
Last edited on
generally, youll use cout for everything unless you have a good reason or habit. I have a habit of using printf because I often need variously formatted doubles and its a huge mess to reformat cout for each entry in a row, and I grew up on printf in an era when C and C++ were often cobbled together interchangeably … back when, c++ and C hybrid code was quite common … a style sometimes referred to as C with cout (and the occasional class or c++ header tool) … before the STL was even dreamed of...

later, you may not use any of those, as you will be programming into a gui or something for most normal programs. But for now, I strongly advise sticking to cout.

A side note, if you want speed, you may even have to roll your own. The are ALL poor at some things, like int to string is poorish across the board and many people have written faster code doing it by hand. Some compilers' string to numbers are not too hot either.

--note to the below comment-- Security is a huge deal as stated. I tend to ignore it because 99% or more of my code isn't network aware. The few things I have that are networked, we have to run it thru a checker that complains mightily if using vulnerable functions and they have to be taken out if used. The joy of writing code just for yourself, you can cheat a little :)
Last edited on
Core Guidelines:

Prefer iostreams for I/O

Reason: iostreams are safe, flexible, and extensible.

Discussion: iostreams vs. the printf() family It is often (and often correctly) pointed out that the printf() family has two advantages compared to iostreams: flexibility of formatting and performance. This has to be weighed against iostreams advantages of extensibility to handle user-defined types, resilient against security violations, implicit memory management, and locale handling. If you need I/O performance, you can almost always do better than printf().

gets(), scanf() using %s, and printf() using %s are security hazards (vulnerable to buffer overflow and generally error-prone). C11 defines some “optional extensions” that do extra checking of their arguments. If present in your C library, gets_s(), scanf_s(), and printf_s() may be safer alternatives, but they are still not type safe.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rio-streams


C++ Coding Standards: 101 Rules, Guidelines, and Best Practices (Sutter and Alexandrescu):

Avoid using varargs in your functions' signatures. Avoid calling functions with varargs in their own signatures, including legacy functions and standard C library functions such as sprintf.

Admittedly, calls to sprintf can often look more compact and easier to read than equivalent calls using stringstream formatting and operator<< = just like it's also admittedly easier to hop into a car without pesky safety belts and bumpers. The risks are just not worth it. printf-related vulnerabilities continue to be a serious security problem at the time of this writing, and an entire subindustry of tools exists to help find such type errors.

Prefer to use type-safe libraries that support variable arguments using other means. For example, the [Boost] format library uses advanced C++ features to combine safety with speed and convenience.
Thanks for the responses..

I normally used cin/cout too for C++. So, is it possible that advanced programmers mixed the two keywords in their codes when they both want performance and extensibility?

@Ganado
This is the SO link :
https://stackoverflow.com/questions/2872543/printf-vs-cout-in-c




C++17 has two high performance alternatives
("intended to allow the fastest possible implementation that is useful in common high-throughput contexts").

std::to_chars https://en.cppreference.com/w/cpp/utility/to_chars
std::from_chars https://en.cppreference.com/w/cpp/utility/from_chars
So, is it possible that advanced programmers mixed the two keywords in their codes when they both want performance and extensibility?

anything is "possible" but most likely you have someone who is comfortable in C and C++ or older era C++ where mixing was more commonplace. The mixing could be copy/paste from C and C++ into one file, or it could be old code, or an older coder, or a confused new coder, or any of many other combinations.

I guess take this as a warning: just because code is out there on the web, does not mean it is good. The web dates back to the early 90s and you can still run across code from then, and not everything posted is really that good (same as some books have terrible code). You can get an algorithm or working blocks of code that serve as a starting place or how-to or may even be usable as-is but take anything you get online as questionable and spend the time to verify (and correct or get alternate code) that it has good style, isnt buggy, and so on before using it.
Topic archived. No new replies allowed.