tags:

views:

943

answers:

10

I know that C++ has the concept of objects but C doesn't. I also know that pretty much all there is to know about C fits into K & R but the C++ library is vastly more complex. There have got to be other big differences though.

What are the major differences between C and C++?

+12  A: 

I think you answered your own question: Objects

Its a completely different design paradigm. I think the confusion comes because many people develop C++ programs that are basically C programs and don't even realize it.

Which allows, in the word of its creator, Stroustrup:

Edit: added some other fun stuff

Paperino
Upvoting because of "...many people develop C++ programs that are basically C programs and not even realize it."
overslacked
+7  A: 

The C++ language says they are the same:

int C = 0;
assert(C++ == C);
Brian R. Bondy
hyuan
lolThat's not true though. The result of your equality test is undefined... :)I wonder what that says about the two languages. :p
jalf
gcc and vc++ both say it's true
Brian R. Bondy
+17  A: 

Check out Stroustrup's FAQ here, specifically:

What is the difference between C and C++?

C++ is a direct descendant of C that retains almost all of C as a subset. C++ provides stronger type checking than C and directly supports a wider range of programming styles than C. C++ is "a better C" in the sense that it supports the styles of programming done using C with better type checking and more notational support (without loss of efficiency). In the same sense, ANSI C is a better C than K&R C. In addition, C++ supports data abstraction, object-oriented programming, and generic programming (see The C++ Programming Language (3rd Edition)"; Appendix B discussing compatibility issues is available for downloading).

Andy Mikula
+2  A: 

Templates is another big difference (in addition to classes/objects). Templates enable for example typesafe generic container types (their first use-case) and (bastardized) lambda-expressions (boost::lambda).

C++ is a much bigger language (not just library) than C.

Magnus Hoff
+1  A: 

This question doesn't have short answer.
In general C++ supports:
- OOP paradigm;
- generic programminng;
- template methaprogramming;
- Abstract data types;
- New libraries and standard supports elemnts of functional paradigm;
- other tools for make your program most supportable;
- Also you could write programs on C-like style but use C++ compiller;


But pure C - a little faster than C++ and more low level.

bb
C faster? What gives you that idea? Are you saying that if I take a C program, feed it to a C++ compiler, it magically becomes slower? Or perhaps that C++'s std::sort is slower than C's qsort (hint: it isn't). C++ has a lot of tools to increase performance far above C.
jalf
Of course, it also enables features that, when used, introduces some overhead. But you don't have to use them, and often, using them is no slower than implementing the same yourself in C.It's meaningless to compare the "speed" of two languages.
jalf
Yes, I agree with "It's meaningless to compare the "speed" of two languages", but I sad "little faster" in general, because in pure-C you cna't use slowly features and you will work only with POD types.
bb
C does support generic programming - look at qsort, which allows arbitrary comparison functions. It just sacrifices type safety to do so.
Tom
C also supports OO programming. Examples from POSIX... A socket is-a file descriptor. A pipe is-a file descriptor. read() and write() are functions that operate on all file descriptors. This is every bit as OO as C++.
Tom
@Jalf standard C++ didn't have `restrict` until 0x, which C had since 99, so there were plenty of cases where the compiler could not safely apply optimisations to standard C++ code but could apply them to optimise C99 code.
Pete Kirkham
@Pete: that's a fair point. But I still think it's wrong to say that C is (or was) faster as a whole. That implies that simply taking a program in language X and feeding it to a compiler for language Y, you gain a speedup. C has at least one feature that C++ lacks, which affects performance, but you have to actually use that feature. It's not the language as a whole that's magically faster. Just like C++ has features that at the very least makes it much *easier* to write fast code than their C equivalents. (Expression templates, or just standard library algorithms such as `sort`)
jalf
+5  A: 

In short, C aspires to be a "portable assembler language". It keeps things simple, lets you do things that map almost directly to the underlying hardware, and doesn't present a lot of high level abstractions (you've got functions and.... that's about it)

C++ tries to be everything. A high level language, a low level language, an object oriented language, a multi-paradigm language, a systems programming language, an embedded programming language, and an application development language.

The two language really don't have much in common, other than some shared syntax. A C program might compile as C++ with only minor changes, but it'll have nothing in common with "proper" C++ code written for the language.

jalf
You forgot about a religion in Your list of what C++ tries to be :)
Maciej Hehl
+1  A: 

Another feature C++ has over C is exception handling in the form of throw ... catch.

Trent
+2  A: 

C++ is much more than C with classes. There are many other concepts inside of C++ like templates, function and operator overloading, exceptions and many others already mentioned here. This makes C++ very powerful and flexible, but also hard to learn. It's not that the single concepts are difficult to understand, but the sum of them and how they are playing together. Take a look on boost to see what everything is possible to do with C++. And I guess it tooks ages to understand what happens under the hood, which is very clear in the case of C.

Or to put it in a nutshell: C++ is much more then C with classes, or in other words C++ is much more then Java plus memory management.

quinmars
+1  A: 

Here's a website showing the "incompatibilities" between c and c++:

http://david.tribble.com/text/cdiffs.htm#C++-vs-C

There are actually quite a few areas where c and c++ diverge (in addition to the classes, template, exceptions, etc).

As far as major differences, here's a list which covers it well:

  • anonymous unions
  • classes
  • constructors and destructors
  • exceptions and try/catch blocks
  • external function linkages (e.g., extern "C")
  • function overloading
  • member functions
  • namespaces
  • new and delete operators and functions
  • operator overloading
  • reference types
  • standard template library (STL)
  • template classes
  • template functions
Evan Teran
A: 

In 1st of 2 parts of C++ for C programmers, some non-OOP differences are listed:

  • Default arguments to functions
  • Function overloading
  • You can declare local variables wherever in the code you need them, not just at the start of a function (also a C99 feature)
  • References, as in int& x
  • Const-correctness in general
  • Namespaces
  • The built-in boolean type, bool, with true and false keywords (also in C99 via stdbool.h)
  • The inline keyword and associated quirks, including implicit inlining and auto-inlining
  • A much larger standard library than C
Dinah