Why should you avoid using C in C++?

I can see why using the standard library is clearly better but how come when some people are used to C people will explicitly tell them to use the C++ equivalent. One example of his is like #define vs const.
Last edited on
Most of the time it will come down to the style guide that has been implemented at that organisation.
Sometimes it's more logical to do it in C++, but I believe it's rather illogical to purposefully avoid using C even if it seems preferable in certain situations.
C is a different language. Many of C constructs and idioms that compile in C++ do not interact all that well with normal C++ code. Of course there are exceptions (some syntax is truly shared, some C idioms are useful in C++), but to use your example, #define used instead of a named constant (which C lacks) tramples all over C++'s scoping rules. Not to mention debugging difficulties, etc.
I think that c lacks classes also
I think it's more of a thing where you shouldn't use C because C++ purposefully found a better solution. When using a C++ compiler chances are you should use const rather than #define . There's plenty of utilities I see used in C++ that came from C though. I could be wrong, but it seems that those who start in C++ eventually get better at coding and are more ready to use C in their code. If people like to accuse C++ of being dangerous though, well C is just as ( and to some more) tedious. I don't use C a whole lot but I have found it useful from time to time. I know a guy who's a hobbyist game programmer though, and he uses A LOT of C, even though he started out in C++.
Last edited on
C code was implemented in C++ just to ease moving C programs into C++.

if it's not used for this purpose then it's bad programming practice.
nowadays, new C++ compilers don't accept some C code in them.

uhh nuts, who am i kidding, i actually use some C code in my programs.
if the exit() function is actually C-function, then i will always be using C in my C++ projects, and i use lots of C-functions simply because i need them.

please don't consider this a misleading comment.
Last edited on
C++ can be dangerous if you dont know what you're doing. C is "worse" in that respect.
closed account (ypfz3TCk)
Well if you use c syntax in a C++ source file it is bad style. Someone looking at it will expect to see c++ code! Your program might therefore be harder to understand if other people are going to be looking at the code.
On the other hand if you write a program for yourself then you can use whatever syntax pleases you - so long as it can be compiled. But the question is why say a program is c++ if it really is c?

I have seen c++ programs where people have used scanf & printf and c headers instead of the c++ cout & cin. There is no good reason to do that and call it a c++ program - when in reality it is a c program in a c++ shell.
In regards to the use of #define and const, defines run the risk of adding hard to find bugs into you program due to them not having a type. It is better practice to use const instead due to them having a type.

In regards to not being able to use C in C++, when did this happen? They converted all the C libraries to C++ equivalents. So now instead of including stdlib.h C++ has you do #include <cstdlib> 1.

Avoid C in C++? That is an oddity question depending on who you ask. There are still a lot of programmers that think you have to learn C before ever learning C++. Then you have those who say never touch C before C++, but rather learn C later. Just depends on what you want I supposed.
1. http://www.cplusplus.com/reference/
Last edited on by closed account z6A9GNh0
if the problem is only with the #define and const, actually a const is better to make constants that will actually be USED in you program, but define is still usable in conditional compilation.
i actually have this style in programming: i write an algorithm, then decide its bad, so i delete it.
a few hours later i discover that the algorithm really needs just a little modification, and it becomes perfect, then i have to write the whole algorithm again.
with conditional compilation, i just enclose the algorithm in such line:
#ifdef _FIRST_WAY
rest of the program

this way, i don't have to delete the algorithm, just exclude it from compiling.
i think this is a good usage of C syntax.

if i'm wrong with something, please show me the right way.
@Rechard3: version control systems can help with that kind of thing without cluttering your source with deprecated code. Also I think using block comments /*...*/ would be better than using #ifdef s
thanks for the advice Lachlan Easton
Topic archived. No new replies allowed.