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 people have said that C++ is a completely different language than C, but Bjarne himself has said that C++ is a language that is extended from C hence that is where the ++ comes from. So why does everybody keep saying that C and C++ are completely different languages? In what way is C different from C++ other than the extended features in C++?

share|improve this question
    
good question (gave you a star) –  Dark Templar Oct 10 '11 at 20:48
1  
Due to how they are used. You can certainly write C in C++... but you shouldn't. –  Ed S. Oct 24 '11 at 18:56
add comment

8 Answers

up vote 6 down vote accepted

At the very beginning, mid 1980's, C++ was almost a proper superset of C. That is how it all started.
But over time, both C and C++ evolved and have diverged a little bit, even though compatibility between the languages is always considered important.

And additionally, the technical differences between C and C++ have made that the typical idioms used in those languages and what is considered 'good practice' has diverged even more.
This is the driving factor behind people saying things like "there is no language C/C++" or "C and C++ are different languages". Although it is possible to write programs that are acceptable to both a C and a C++ compiler, the code is generally considerd to be neither an example of good C code nor an example of good C++ code.

share|improve this answer
    
I don't think the first paragraph is correct. I believe C always had implicit casting from void * but C++ never did. (Did not downvote) –  alternative Apr 10 '11 at 16:50
2  
@mathepic: Define "always". C took void* type from C++. In K&R C, malloc was returning char* –  Nemanja Trifunovic Oct 24 '11 at 18:53
add comment

Stroustrup himself answers that in his FAQ:

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.

It's support for object-oriented programming and generic programming that make C++ "completely different" to C. You can almost write pure C and then compile it with a C++ compiler (as long as you take care of the stricter type checking). But then you're still writing C - you're not writing C++.

If you're writing C++, then you're making use of it's object-oriented and template features and that's nothing like what you would see in C.

share|improve this answer
add comment

Put simply, what is considered idiomatic in C is definitely not idiomatic in C++.

C and C++ are very different languages in practice, because of the way people use them. C aims at minimalism, where C++ is a very complex language, with a lot of features.

There are also some practical differences: C can be easily called from pretty much any language, and often defines the ABI of a platform, whereas C++ is quite hard to use from other libraries. Most languages have a FFI or interface in C, even languages implemented in C++ (java, for example).

share|improve this answer
    
Thanks. Very concise yet informative. –  jpartogi Jan 22 '11 at 12:14
    
It's not just that C-style code is non-idiomatic in C++. C-style coding is actually problematic in C++, because of the lack of exception safety. –  dan04 Jan 22 '11 at 13:38
    
Wait, so why is it that C is easily ported to other languages where as C++ isn't? –  Dark Templar Oct 10 '11 at 20:38
    
@DarkTemplar: What on earth do you mean? –  DeadMG Oct 24 '11 at 17:21
add comment

Apart from the obvious fact that C++ supports object-oriented programming, I think you have your answer here: http://en.wikipedia.org/wiki/Compatibility_of_C_and_C++

That article contains code examples showing stuff that is ok in C but not in C++. For instance:

int *j = malloc(sizeof(int) * 5); /* Implicit conversion from void* to int* */

Porting a C program to C++ is often straightforward and consists mostly of fixing compilation errors (adding casts, new keywords etc).

share|improve this answer
    
great answer! +1 –  Dark Templar Oct 10 '11 at 20:48
1  
Porting a program like that does not give you a C++ program. It gives you C that can be compiled on C++ compiler. That does not make it C++ (I would still call the resulting code C (not even C with classes)). –  Loki Astari Oct 24 '11 at 17:46
add comment

The difference is that in C you think procedurally and in C++ you think in an object orientated way. The languages are quite similar but the approach is very different.

share|improve this answer
    
Doesn't C also have structs? Aren't C files themselves separate modules(basically, objects)? Not sure what the exact difference between procedural and object-oriented is... –  Dark Templar Oct 10 '11 at 20:39
1  
Dark you have illustrated my point. It's not a limitation of the language that allows you to write procedurally or in an object-orientated way it is an ethos or way of thinking. –  Ant Dec 14 '11 at 9:29
add comment

C++ adds not only new features, but new concepts and new idioms to C. Even though C++ and C are closely related, the fact remains that in order to write effectively in a language, you must think in the style of that language. Even the best C code cannot take advantage of the different strengths and idioms of C++, and so is more likely than not actually rather bad C++ code.

share|improve this answer
add comment

The "extended features", you make it sound like in C++ they added like, variadic macros or something and that's it. The "extended features" in C++ are a complete overhaul of the language and totally supersede the best C practices because the new C++ features are so much better than the original C features that the original C features are completely and totally redundant in the vast majority cases. Suggesting that C++ merely extends C is suggesting that a modern battle tank extends a butterknife for the purposes of waging war.

share|improve this answer
    
Can you enumerate an example of one of the more useful features that C++ has that C doesn't? –  Dark Templar Oct 10 '11 at 20:46
    
@DarkTemplar: How about easy-mode resource management with RAII? Or nice generic data structures using templates? Just to begin with. –  DeadMG Oct 11 '11 at 10:33
add comment

While C++ can be a super set of C in syntactic terms - i.e. any construct of C program can be compiled by C++ compiler.

However, you almost never write a C++ programs the way you would have done with C program. The list can be endless or may be someone should just do more research to put it as exhaustive reports. However, i am putting few pointers that make the key differences.

The point of the current post is that C++ has following features that a good C++ programmers must use as programming best practices even though C equivalent are possible to compile.

How should it be done in C++ over C

  1. Classes & Inheritance. That's most important differences that allows systematic object orientation that makes programming expression very powerful. I guess - this point needs no better explanation. If you are in C++ - almost always, you are better of making use of classes.

  2. Privatization - Classes, and even structures have what is private members. This makes encapsulation of a class possible. The equivalent in C is by way of typecasting the object as void * to application so that application doesn't have access to internal variables. However, in C++ you can have elements with public as well as private classes.

  3. Pass by reference. C++ allows modification based on reference, for which passing pointers is required. Pass-by-reference keeps code very clean and more so safer against pointer hazards. You an as well pass C style pointer and that works - but if you are in C++ you are better-off as long as

  4. new & delete vs. malloc and free. The new() and delete() statements not only allocates and de-allocate memory, but also allow code to execute as part of destruct-er to be called in a chain. If you are using C++ - it is actually BAD to use malloc and free.

  5. IO types and operator overloading Operator overloading make code readable or more intuitive if done well. Same for << and >> io operators. The C way of doing this would be to use function pointers - but it is messy and only for advance programmers.

  6. Using "string". The char * from C works everywhere. So C and C++ is pretty much same. However, if you are in C++ - it is always much better (and safer) to use String classes which saves you from the dangers of arrays over running which are almost every things.

Features I would still not be fan of in C++ 1. Templates - While i don't use heavy templates in many codes - it can turn out to be very powerful for libraries. There is almost no equivalent of it in C. But on a normal day - specially if you are doing mathematically missing.

  1. Smart pointers - Yes, they are very smart! And like most smart things - they start out good and become messy later! I don't quite like to use

Things that i like about C and miss in C++

  1. Polymorphic algorithms by using function pointers. In C, when you are running complex algorithms - some times you can use set of function pointers. This makes true polymorphism in a powerful way. When you are in C++ you CAN use function pointers - but that is bad. You should be only using methods - else be prepared to get messy. The only form of polymorphism in C++ classes is function and operator overloading but that is quite limiting.

  2. Simple threads. When you create threads were pthreads - it is quite simple and manageable. It becomes when you need to create threads which are supposed to be "private" to the classes (so that they have access to private members). There is boost type of frameworks - but nothing in basic C++.

Dipan.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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