45

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++?

gablin
  • 17,525

5 Answers5

46

The following points relate to C++:

  1. (user-defined) static type system: allows static checks about your data and their usage - points a lot of easily done errors in C.
  2. multi-"paradigm": allows 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: allows 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 has to manage it. Exceptions 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 doesn't make the whole program wrong - if done well, again.
  9. The Standard Library: C has its own, but it's all "dynamic". The C++ standard library is almost (not IO streams) made of templates (containers and algorithms) that allows generating code only for what you use. Better: as the compiler has 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 its code - thanks to templates and other things.
  10. const-correctness: The best way to make sure you don't change variables you shouldn't. Allows to specify read-only access to varaibles. And it is only checked at compile time so there is no runtime cost.
Klaim
  • 14,902
  • 4
  • 51
  • 62
33

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.

Jas
  • 6,313
16

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.

Federico klez Culloca
  • 3,084
  • 25
  • 24
10

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.

Jerry Coffin
  • 44,795
2

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

zvrba
  • 3,480