Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
1  
I thought C++ is super set of C –  user Jan 10 '12 at 11:01
1  
C++ is not enhanced... its a superset of C.. –  Joe DF Mar 7 '13 at 16:53
add comment

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.

6 Answers

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
1  
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. –  drak0sha Jan 19 '11 at 20:54
2  
@Vlad, none of what you are talking about was an option 25 years ago. –  Jas Jan 20 '11 at 8:06
1  
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
add comment
  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
add comment

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
1  
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
4  
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
1  
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
show 3 more comments

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
3  
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: arxiv.org/abs/1003.2547 –  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 at 23:18
add comment

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 in the library that's not present in the current C++ standard; most of this has been added to C++0x though.

In terms of basic orientation, C basically supports procedural programming. C++ supports that as well as object oriented programming, generic programming, and metaprogramming (carrying out arbitrary computation at compile time). With C++0x, it adds a few bits and pieces that could at least be mistaken for functional programming support as well.

share|improve this answer
add comment

C++ grafted object-oriented programming onto C [insert Frankenstein's Monster joke here]

C is a subset of C++

share|improve this answer
4  
"C is a subset of C++" - this is not true. There are a number of things you can do in C which you cannot do in C++ at all. –  JBRWilkinson Nov 4 '10 at 11:05
1  
@JBRWilkinson technically yes, but if you look at that list of things (added mostly in the C99 standard), they're not really very important. The OP asked about fundamental differences, and from that perspective there aren't any. Stroustrup developed C++ initially as a preprocessor for C. C was a strict subset of C++ until the C99 standard (according to google). But for practical purposes, C++ is still C with objects grafted onto it. www2.research.att.com/~bs/C++.html –  Steven A. Lowe Nov 4 '10 at 14:47
add comment

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