views:

944

answers:

7

For those of you with experience with both, what are the major differences? For a newcomer to either, which would be better to learn? Are there situations where you might choose C but then other situations where you would choose C++? Is it a case of use the best tool for the job or one is significantly better than the other. I know C++ is an "enhancement" of C, but it was created in '83 and hasn't completely replaced C so there must be something more to it.

I know this question is subjective and I am not trying to start any religious war, so please try to be as objective as possible. Clear strengths and weaknesses and comparisons.

+8  A: 

C++ is 99% a superset of C. It's a little more strict in syntax, with a few very minute differences in terms of things changing.

The biggest difference is that C++ makes an attempt at being object oriented. There's native support for classes.

There's a few other perks in C++: templates, stream operators, pass-by-reference (a bit less confusing than pass-by-pointer)

What do you lose out for going C++? It's missing some of the lowest-level hacks that a lot of people use C for. I don't remember any of them offhand, but I've never heard any good argument for tricking the compiler into doing what you want except as a way to push efficiency that extra 10%.

I believe 'extern "C"' lets you get at anything 'c' can do in any case - and generally the same compiler can do both.
frankodwyer
+6  A: 

C++ is, as it's name implies and as you said in your question, an enhancement of C. It's a significant enhancement. (And I use the term 'enhancement' to refer to features, not to function.) The thing about enhancing, though, is that it means growth. C++ typically lends itself to much bigger programs. Applications, really. It's a high performance language, but it's big.

C, on the other hand, is used for kernel and driver programming for a reason. It's old (ancient?), small, and if you're smart, about as fast as you can get without writing assembler yourself. The tradeoff, obviously, is features. C doesn't have a lot of the nice big squishy concepts like classes and templates that C++ programmers like myself take for granted (yep, totally guilty).

So to answer your question more directly, most of my large, high performance projects get written in C++. If I'm working on something like a driver or an embedded system, I'll expect to be using C.

Sean Edwards
awesome. So to make sure I understand, you reach for C when you want very small or want to squeeze every performance benefit out of it that you can? is that a fair statement?
Ryan Guill
Pretty much. I personally reach for C when I'm doing embedded programming. Otherwise I use C++. For desktop applications - even high performance ones like games or distributed servers - the hardware is powerful enough to make it really just a matter of personal preference. I happen to prefer C++.
Sean Edwards
Nope. You can do that with C++ too. Its more about simplicity than performance.
Loki
Oh, I certainly didn't mean to imply that you can't use C++ for an embedded system. If you have the right compiler, you can use C++ for anything you can use C for. But when you measure your memory in kilobytes, C++ isn't always best. :)
Sean Edwards
The irony of the name using the post-increment rather than the pre-increment operator is that it implies that C++ is not quite a step up from C (yet).
JohnMcG
+5  A: 

If you've never used a language that requires you to do manual memory management I would go for C first.

Concentrate on the C fundamentals like strings, function pointers, and how memory is used and managed. These will all transfer when you make the transition to C++. Above all else, make sure you really grok pointers, how they relate to memory, and the relationship between pointers and arrays. I would say to be a well-rounded programmer understanding these things is required.

Then, go to C++ and learn about the OO model, templates, etc. Trying to do everything in C++ from the very beginning can be a little overwhelming.

I have experience with oo languages (java for one). But are you saying you still have the manual memory management and such in C++, but it would be better to learn things like that from C first?
Ryan Guill
Yes, C and C++ require you to manage the memory yourself. No garbage collection. I think it's easier to learn these types of fundamentals from C. The language is smaller and there are less syntactic elements to get in the way when you're learning the fundamentals.
Awesome. Thanks for the clarification.
Ryan Guill
totally right, C and C++ are awesome in power; but it demands respect and understanding. you really have to know deep in your guts what's happening down there.
Javier
I simply don't agree that you should learn C first. What will really be overwhelming is learning to deal with C strings, catching needless errors that would have been caught by a strongly typed compiler, and debugging memory problems that could have been prevented by using the STL.
Jason Baker
@Jason BakerThe other side of that argument is that working through those types of errors and understanding why they occur is precisely what gives you a good understanding of the guts of C and C++. For getting work done, you're right, for learning I think doing those things is actually beneficial.
@stbuton: agreed, completely. I learned C first (C++ wasn't available when I started), and it helped me to no end in understanding C++ later. Learning assembly language would probably have been slightly better, but I doubt I'd have had the patience for that.
Head Geek
+11  A: 

While C is a pure procedural language, C++ is a multi-paradigm language. It supports

  • Generic programming: Allowing to write code once, and use it with different data-structures.
  • Meta programming: Allowing to utilize templates to generate efficient code at compile time.
  • Inspection: Allows to inspect certain properties at compile time: What type does an expression have? How many parameters does a function have? What type does each one have?
  • Object oriented programming: Allowing the programmer to program object oriented, with sophisticated features such as multiple inheritance and private inheritance.
  • Procedural programming: Allows the programmer to put functions free of any classes. Combined with advanced features such as ADL allows writing clean code decoupled from specifics of certain classes.

Apart from those, C++ has largely kept compatibility with C code, but there are some differences. Those can be read about in Annex D of the C++ Standard, together with reasons and possible fixed to make C code valid C++ code.

Johannes Schaub - litb
Thanks for the reference to Annex D. That will be helpful.
Ryan Guill
you're technically right, but C++ is still so heavily procedural, that all the other 'paradigms' (more like design styles) are only useful in the subset that can be shoehorned inside the procedural mindset. not that it's incomplete, just that it's too awkward if you go too far from proderural.
Javier
i think trying to program only object oriented, only using meta programming or only procedural in C++ isn't good (for the latter - why use C++ at all then). one would try putting static functions into a class consisting of nothing else - while one should put functions into a namespace, for example.
Johannes Schaub - litb
+3  A: 

I would make the argument that you would be better off using C++ over C in most cases. You don't have to use all of the complicated features of C++ if you don't want to. There are a few things that C++ add that are really helpful for most cases:

  • Stronger typing.
  • A string class included in the standard library.
  • An array class (vector) that grows as you need it to and handles all of the allocation and deallocation of memory for you.

Personally, I feel that those three things make using C++ worth it even if you use it to write C-like code (aka procedural, non object-oriented code).

Maybe if you're doing some kernel hacking or embedded systems development you should use C, but otherwise, I'd recommend C++.

Jason Baker
+2  A: 

For those of you with experience with both, what are the major differences?

C is a subset, C++ is a superset. C++ includes features to support object-oriented programming (e.g. "polymorphism"), and many other features.

For a newcomer to either, which would be better to learn?

C is easier (because it's a smaller topic), and C++ is better (because it's more powerful, includes C, and in my experience there are more jobs programming in C++ than there are in C).

Are there situations where you might choose C but then other situations where you would choose C++?

I'd choose C over C++ in the rare, rare situations where the target platform supports C but not C++ (i.e. on some embedded devices).

ChrisW
+2  A: 

You use C++ where you can and C where you have to. Generally speaking, if you have a C++ compiler available for your platform, there's no reason not to use that. C is a perfectly good language but C++ adds so much extra without losing you any power, so it would almost always be the language of choice.

Kylotan