For example printf
instead of cout
, scanf
instead of cin
, using #define
macros, etc?
views:
816answers:
11I wouldn't say bad as it will depend on the personal choice. My policy is when there is a type-safe alternatives is available in C++, use them as it will reduce the errors in the code.
It depends on which features. Using define
macros in C++ is strongly frowned upon, and for a good reason. You can almost always replace a use of a define
macro with something more maintainable and safe in C++ (templates, inline functions, etc.)
Streams, on the other hand, are rightly judged by some people to be very slow and I've seen a lot of valid and high-quality C++ code using C's FILE*
with its host of functions instead.
And another thing: with all due respect to the plethora of stream formatting possibilities, for stuff like simple debug printouts, IMHO you just can't beat the succinctness of printf
and its format string.
I would say the only ones that are truly harmful to mix are the pairings between malloc
/free
and new
/delete
.
Otherwise it's really a style thing...and while the C is compatible with the C++, why would you want to mix the two languages when C++ has everything you need without falling back?
On the contrary, I believe features of C have lesser overhead.
For example, printf
vs. cout
Not really, printf()
is quite faster than cout
, and the c++ iostream library is quite large. It depends on the user preference or the program itself (is it needed? etc). Also, scanf()
is not suitable to use anymore, I prefer fgets()
.
For allocations, I would avoid using malloc/free altogether and just stick to new/delete.
What can be used or not only depends on the compiler that will be used. Since you are programming in c++, in my opinion, to maximize compatibility it is better to use what c++ provides instead of c functions unless you do not have any other choices.
You should definitely use printf
in place of cout
. The latter does let you make most or all of the formatting controls printf
allows, but it does so in a stateful way. I.e. the current formatting mode is stored as part of the (global) object. This means bad code can leave cout
in a state where subsequent output gets misformatted unless you reset all the formatting every time you use it. It also wreaks havoc with threaded usage.
There are better solutions for most cases, but not all.
For example, people quite often use memcpy
. I would almost never do that (except in really low-level code). I always use std::copy
, even on pointers.
The same counts for the input/output routines. But it’s true that sometimes, C-style printf
is substantially easier to use than cout
(especially in logging). If Boost.Format isn’t an option then sure, use C.
#define
is a different beast entirely. It’s not really a C-only feature, and there are many legitimate uses for it in C++. (But many more that aren’t.)
Of course you’d never use it to define constants (that’s what const
is for), nor to declare inline functions (use inline
and templates!).
On the other hand, it is often useful to generate debugging assertions and generally as a code generation tool. For example, I’m unit-testing class templates and without extensive use of macros, this would be a real pain in the *ss. Using macros here isn’t nice but it saves literally thousands of lines of code.
Coming from a slightly different angle, I'd say it's bad to use scanf in C, never mind C++. User input is just far to variable to be parsed reliably with scanf.
I'd just post a comment to another reply, but since I can't... C's printf() is better than C++'s iostream because of internationalization. Want to translate a string and put the embedded number in a different place? Can't do it with an ostream. printf()'s format specification is a whole little language unto itself, interpreted at runtime.