C++ vs C

Sep 23, 2011 at 6:14pm
Ok so I'm sure this question has been asked a million times, but what is the practical differences between C++ and C? When would you use one over the other? I understand C is a lower level language and speaks with the hardware much easier and safer, but how? And is there much of a syntax difference between the two?
Sep 23, 2011 at 6:27pm
In C++ you can have classes. That's pretty much it.
Sep 23, 2011 at 6:30pm
I understand C is a lower level language and speaks with the hardware much easier and safer, but how?


Your understanding is wrong. You can program just as low level in C++ as you could in C, just that C++ also has high level language features that C does not have. And "low level access" and "safety" are inversely proportional.


And is there much of a syntax difference between the two?


Not much, most "syntax differences" would be stuff like additional keywords. In general, you could compile most C programs pretty much unchanged with a C++ compiler. (there are certain things that C++ does not support, such as variable length arrays - not sure if they changed that with C++11, I see no reason why they would want to do that though - and the C standard headers have slightly different names to avoid collisions (for example, stdio.h is called cstdio in C++).

When would you use one over the other?


You use C when you want to write a library that's available for both C and C++, for interacting with legacy code and when there's no C++ compiler available. I can't think of any other reasons.
Sep 23, 2011 at 6:33pm
This is sure to be a thread full of different opinions, but here's mine. The difference is that C++ is better. It has more features (built-in support for more paradigms, etc.) than C and offers more, safer, and more extensible capability in its standard library. You can do anything in C++ that can be done in C (and vice versa).

I'm aware of only 3 valid arguments to use C over C++:

1) In existing codebases it may be easier/faster/cheaper to maintain just C.
2) Existing developers don't know C++, or don't understand how to use it in an effective, scalable way.
3) Platform (usually embedded) doesn't have C++ compiler(s) at all or that are stable enough.
Last edited on Sep 23, 2011 at 6:36pm
Sep 23, 2011 at 6:34pm
Oh! I forgot:

There's this nifty thing called new that basically replaces C's malloc() in C++. Similarly C's free() is replaced by C++'s delete

new and delete FTW!
Sep 23, 2011 at 6:41pm
Well, I have done most of my programming in C++, but I keep hearing that I should also learn C. I am currently a CS major, and would like to have as much knowledge as I can. Should I learn C? Or is it close enough that I can just not spend too much time on C specifically and just port my C++ knowledge over into C?
Sep 23, 2011 at 6:46pm
C++ adds a few features, new reserved words, and of course everything I've read says that any C program can compile with a C++ compiler and all C programs are valid C++ programs. The differences are subtle in my opinion.
Sep 23, 2011 at 6:50pm
Learning C is imho a waste of time, unless your employer expects you to use C. When starting a new project, I see hardly any point in choosing C over C++ (again, that is only if a C++ compiler is available for that platform. Many microcontrollers for example only have C compilers available for them).

Sep 23, 2011 at 6:53pm
Hmm, well next semester I have the option of taking a course just on C. Not sure if I should or if I should look into something else
Sep 23, 2011 at 6:56pm
Again - if you already know C++ well, you don't need to learn C. The only "new" thing you'd learn is how to use the C standard library. And you may gain some additional experience in how excessive use of pointers can fuck up your programs.
Sep 23, 2011 at 7:01pm
Hmm, well next semester I have the option of taking a course just on C. Not sure if I should or if I should look into something else

No, don't waste your time. You can start worrying about C when you have to actually work on a C project.
As long as you know what the differences between C and C++ are, you should be able to write C programs if you have to. You just have to keep in mind that memory management has to be done manually, as RAII isn't possible, classes have to be emulated with C structs and free functions, you can't use the STL containers and algorithms, templates have to be emulated with macros where possible, you use error codes instead of exceptions, etc.
Last edited on Sep 23, 2011 at 7:02pm
Sep 23, 2011 at 7:26pm
C++ is a superset of an older C standard. Meaning that C++ surely adds new features to C. But since the point where that started, C has been updated a few times too. Comparing them has since then become a bit more complex.
Topic archived. No new replies allowed.