Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Many tend to write "C/C++", as if they were the same thing. Although they share many similarities, they are clearly not the same.

But what are really the fundamental differences between C and C++? Is C++ an enhanced version of C, or are there features in C which do not exist in C++?

share|improve this question

closed as too broad by gnat, MichaelT, GlenH7, World Engineer Oct 28 '13 at 22:45

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

I thought C++ is super set of C – user Jan 10 '12 at 11:01
C++ is not enhanced... its a superset of C.. – Joe DF Mar 7 '13 at 16:53
@JoeDF It was at the very beginning but it's actually "compatible with C" now which don't mean the same thing at all. You don't code in C in C++ and not all standard C is compatible with standard C++. – Klaim Jun 3 '14 at 8:27
+1 You are right, there are now like brothers or cousins. If you know what I mean. – Joe DF Jun 3 '14 at 18:04
  1. (user-defined) static type system : allow static checks about your data and their usage - points a lot of easily done errors in C.
  2. multi-"paradigm" : allow working like in C, with object-oriented paradigms, with generic paradigms etc.
  3. Constructor/Destructor : the only way to say once what to do when creating or destroying something and be sure the user will not have to find the right function and use it like in C
  4. RAII (badly named) : you don't have to always manage memory. Just keep things in scope and use smart pointers describing your objects lifetime. Still can use raw pointers.
  5. Templates : better than macro, a real language to manipulate and generate types before the final compilation. Only lacks a type system (see Concepts in future C++ standards).
  6. Operator overloads : allow to describe operations in a simple syntactic manner and even to define embedded domain-specific languages inside your C++ code.
  7. Scoped names : namespaces, classes/struct, functions, etc. have simple rules to make sure names don't clash.
  8. Exception system : a way to propagate errors that is often better than return code. In fact, return code are good for domain-specific logical errors, because the application have to manage it. Exception are used for "hard" errors, things that make the following code just incorrect. It allows for catching errors higher in the call stack if possible, react to such exception (by logging or by fixing the state) and with RAII, if well used, it don't make the whole program wrong - if done well, again.
  9. The Standard Library : C have his own, but it's all "dynamic". The C++ standard library is almost (not IO streams) made of templates (containers and algorithms) that allow generating code only for what you use. Better : as the compiler have to generate code, it will know a lot about the context and will hapily apply a lot of optimizations without having to require the coder to obfuscate it's code - thanks to templates and other things.
  10. const-correctness : The best way to make sure you don't change variables you shouldnt. Allow to specify read-only access to varaibles. And is only checked at compile time so there is no runtime cost.
share|improve this answer

C++ was invented to manage complexity that C could not handle. For example, a common problem with C was that you could "run out of names for variables" (not to be taken literally of course) because there was no encapsulation, namespaces etc.

Also, C does not have exceptions, therefore error handling is very error prone, since it depends on the library user to always check return values of funcs, whereas with exceptions, the library developer simply throws an exception that guarantees program flow will be halted.

C++ helps by having the constructor init objects which is automatically called by compiler. Unlike C structs which need to be initialized by the programmer (hence another error-prone area).

Lastly, there is a lot of other advantages touted by OOP , such as object reuse as well the generic programming based concepts, such as templates and generics which allow you to reuse source code,etc.

And a lot of other things that would take too much of my time to list here.

share|improve this answer
I like that you write about C++ constructor vs C structs, and that it is error-prone. I agree with that. But I don't like the Java way of using this in JavaBeans, that always uses an empty constructor and then set the member fields with setters. In my point of view, that is as error-prone as C structs. I would prefer to set my Java Objects only with the constructor. See my question on StackOverflow about this. – Jonas Nov 3 '10 at 22:44
You have a point there, but the focus of my answer here was C vs. C++. – Jas Nov 4 '10 at 0:15
Oh, come on, who prevents you from using OOP with C? You can reuse objects and do anything, even exceptions. There is even a book about it, called OOP programming in C. – user11408 Jan 19 '11 at 20:54
@Vlad, none of what you are talking about was an option 25 years ago. – Jas Jan 20 '11 at 8:06
You CAN do OOP in nearly every programming language still in use, but it doesn't mean the language was designed for it. Take Lua for instance. While it technically allows OOP, it seems there's about fifty different ways of doing it, a cause of much headache. – tyjkenn Jan 4 '13 at 19:02

In general, everything that exists in C is supported in C++. Obviously the opposite is absolutely false.

Simply speaking, C++ is object oriented (so, for examples, you have classes), C is not.

C++ has a boolean type C89 doesn't.

They are different languages. They just share most of the syntax.

share|improve this answer
C99 has a Boolean type (named _Bool, with bool as an alias). – Jerry Coffin Nov 2 '10 at 22:18
This isn't strictly true. For example, C99 has the long long data type which isn't (yet) part of ISO C++. – Chinmay Kanchi Nov 2 '10 at 22:19
Err... C++ is not only Object oriented : you can use object oriented paradigms with C++ because the language provide features for that, but it provide features for other paradigms too. You should mention that, it's really important, it changes everything. If it didn't, we'd all have switch to java... – Klaim Nov 3 '10 at 10:15
There are many constructs in C that do not work in C++. – user4051 Nov 3 '10 at 13:27
@Jerry Coffin , @Chinmay Kanchi did you notice I wrote (ANSI)? – Federico Culloca Nov 3 '10 at 18:43

C99 has a few features that don't exist (at least in exactly the same form) in C++ (e.g., flexible array members, variable length arrays, etc.)

C99 also added a lot to the library that's not present in C++ 98/03 standard; most of this has been added to C++11 though.

In terms of basic orientation, C basically supports structured procedural programming. C++ supports that as well as object oriented programming, generic programming, and metaprogramming (carrying out arbitrary computation at compile time). With C++11, it adds a few bits and pieces that could at least be mistaken for functional programming support as well (e.g., lambda expressions). C++14 has added a few more, but most of them are really more convenience rather than any sort of major change in orientation.

share|improve this answer

Personally, I think that templates are the most significant feature that C++ adds to C.

share|improve this answer
Er, how about classes with inheritance? That's really hard work in C, whereas a lot of Templates can be done with preprocessor Macros. – JBRWilkinson Nov 4 '10 at 11:04
Preprocessor macros are not type-safe; it's pure textual substitution, which also makes debugging harder. To get basic classes and inheritance to work is not a lot of work in C. + you get to make your own metaobject model instead of being stuck with whatever the language designer chose for you. See, for example, this paper: – zvrba Nov 4 '10 at 12:36
My vote would be destructors for the most significant feature C++ has over C (even over constructors because of their amazing cleanup capabilities). – Thomas Eding Apr 14 '14 at 23:18
@zvrba #define GENERATE_INTERFACE(T) T T##_func(T x);; (type) safe overloading/templates in C. I agree with Thomas that destructors are a far more important feature C is missing. But destructors often hide important code. Namespaces (scope) are in my opinion the most important. – YoYoYonnY Apr 21 at 19:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.