user461697
user461697

Reputation:

Which alternative in C++?

One of the things about programming is that there are several ways to achieve the same goal. During my time coding in C++ I have noticed the following variations in ways to implement certain common features. My question is what are the advantages/disadvantages of each, I understand for the most part how each of them work, but I would like to know which is better in terms of performance, readability, and conformity to standards, as well as acceptability with OOP.

cout vs printf

cin vs scanf

endl vs \n

Thanks

Upvotes: 0

Views: 883

Answers (4)

Chubsdad
Chubsdad

Reputation: 25537

If you were to ever debug the calls to cout, cin, endl etc in your debugger, you will find that these are surely convenient OO wrappers over the standard C library. Definitely, these would be at best equal in performance to their C library counterparts most of the times. But as other responses point out, these objects/techniques in C++ have their own style, advantages which far outweigh the performance in most situations.

As an example consider the number of security bugs that are introduced due to improper/careless use of 'printf'!

Most C++ programmer are also used to see cout, cin, endl etc instead of their C counterparts like printf, scanf and so on. So definitely readability improves if a C++ programmer sees what he likes to see. But that really depends on which team you are on. If it is primarily C developers who are migrating to C++, there will be a initial teething phase to get used to this C++ style.

Upvotes: 1

sbi
sbi

Reputation: 224179

C's IO and C++' IO share little besides the fact that they both do IO. While C's IO capabilities require programmers to pay very close attention to what they're doing to always match the formatting string with the actual parameters passed, or the code will invoke undefined behavior. C++' IO will do the right thing automatically for whatever types you throw at it.
Besides the very few spots where IO performance really matters, I really don't see the point of using C's IO. (And, no, I don't count familiarity with the printf() formatting strings as a good reason to prefer an unsafe feature over safe one.)

As for '\n' vs. std::endl: The latter is a convenient wrapper for << '\n' << std::flush. IME streams rarely ever need to get flushed, so you should use '\n' by default.

Upvotes: 1

rerun
rerun

Reputation: 25505

You will find in a lot of commercial code both intermixed. While this is not good style it will often come down to if the person who last edited the code was originally trained on c or c++. If you can stick to the c++ style operation you get a lot of bang for your buck in the long run with operator overloading and polymorphism as well as less errors in your code.

In any case you will be most likely linking to external libraries that use more c style semantics so in any large c++ project can expect to see a mixture of c++ OO programing and more functional c style.

Upvotes: 0

kartheek
kartheek

Reputation: 771

if your primarily programming lang is C++ and you are doing object oriented programming, then it is going to be cout, cin and endl - hands down :)

it is not a matter of performance, but it is more of a matter of style and coherent coding.

in addition to that you get the perks of having cout, cin work on user defined types as well -- something that is not possible with printf scanf.

Upvotes: 6

Related Questions