C++ is NOT
C.
Many programmers believe that
C++ is no more than
"a better C". Take a dash of
C "add on"
Object Oriented Programming and
voila! ---
C++ emerges!
Now don't get me wrong - that is just how
C++ started off, and older
C++ programs reflect this legacy. However,
C++ has long since evolved out of its humble beginnings into a modern language of it's own right.
The avatar of
C++ we see today is a far cry from the plain and simple
C it originated from and in this post I will try to provide some insight into the differences between the two languages.
First, let's start with some
Adjectives To Describe Each Language. This will give you a flavor of the differences between the two languages. Ready? Here goes.
adjectives.txt
ADJECTIVES THAT DESCRIBE
CCCCCCC CCCCCCC++
CCCC CCCC
CCCC Fast CCCC Powerful
CCCCC Efficient CCCCC Generic
CCCCC Simple CCCCCC Complex ++ ++
CCCCC Raw CCCCCC Object Oriented ++++ ++++
CCCCC Small CCCCCC Tricky ++ ++
CCCC Quirky CCCC Massive
CCCC Primitive CCCC Flexible
CCCC CCCC
CCCCCCC CCCCCCC++
(in no particular order)
After whetting our appetite with the list we look at another way to see differences between the two languages. Let us check the traditional
"Hello World!" program in each language:
hello_world.c
/* C-version: No header, chained functions, will not compile as C++ */
main () { return printf ("Hello world!\n") ? 0 : 1; }
hello_world.cpp
// C++ version: Namespaces, no-extension headers, operator overloading
// void main, stronger type declarations, will not compile as C
#include <iostream>
int main (void)
{
std::cout << "Hello world!"
<< std::endl;
return std::cout.fail () ? 1 : 0;
}
From the above descriptions we should begin to see that although
C and
C++ have a
lot in common they also have
lot of differences.
SIMILARITES
Both
C and
C++ share the basic
ALGOL syntactic structure. Additionally,
C++ contains (almost) all the semantic concepts of
C. Many valid
C programs are also valid
C++ programs (the
"Hello World!" example above, however, is not).
DIFFERENCES
The main differences between the two languages is in
approach and
philosophy.
C strives to be
simple and
elegant which
C++ strives to be
powerful and
type-safe. This fact has directly lead to the wide gulf in the two closely related languages.
C designers worked to make a program that was simple to compile. The
C community is also more conservative and tend to follow the
Principle Of Least Astonishment. This leads to programs that are simple to understand but tend to have large amounts of branches, loops, and macros (and large-ish functions).
C++ designers, on the other hand, delight in making the life of the compiler writer difficult.
C++ must consume every "cool" feature that comes out in any language. The
upcoming C++0x standard includes
Type Inference,
Garbage Collection,
Lambda Functions, and the
Kitchen Sink!
The creator of
C++, Bjarne Stroustrup, at one time
famously said:
"Within C++, there is a much smaller and cleaner language struggling to get out."
But we should take that with a pinch of salt. Now-a-days the
C++ community is very happy with their humongous language thank you very much.
In the words of
Donald Knuth:
"Whenever the C++ language designers had two competing ideas as to how they should solve some problem, they said, 'OK, we'll do them both'"
C contains few surprises while
C++ is a massive behemoth of edge cases and tricky trade-offs. On the other hand,
C++ is far more expressive and, if used with care, far more powerful.
To conclude this post here are some more points of difference between
C and
C++. It should help underline the divergence of the two languages.
C++ programmers use
type-safe templates while
C programmers tend to use the untyped
void pointer.
- In it's quest for even greater type-safety C++ introduced new typecasting operators and discourages the use of the C casting operator.
-
typedef's in C++ share the same namespace as structures, unions, and enums. In C they are distinct namespaces.
- In C,
sizeof('a') == sizeof(int) while in C++ sizeof('a') == sizeof(char).
- C distinguishes between the declarations
int f(); and int f(void);. The latter is declares a function taking no parameters, while the first declares a function that takes an uspecified number of parameters. C++, on the other hand, makes no distinction between the two declarations and considers them both to mean a function taking no arguments.
- Nested structure tag scope is global in C and scoped to the containing structure in C++.
- C supports K&R-style function definitions (but nobody cares anymore).
- C allows a string constant initializer to contain exactly one more character than the array it initializes, i.e., the implicit terminating null character of the string may be ignored. C++ always includes the terminating null character in the initialization.
- C++ prefers to do error reporting through exceptions while C uses function return values as an error signal.
- Gratuitous use of operator overloading, and references can make it harder to understand the side effects of C++ code. On the other hand, judicious use of these features can make for smaller and more expressive code.
- This is a very interesting post about the differences between
inline in C and C++.
- This page has a superbly comprehensive list of incompatibilities between C and C++.
TL;DR:
C++ started off as a better(?)
C but has evolved into a distinct language.