Bryan's Blog


What the heck is C/C++?

I see this all the time in programming forums, websites, etc. My one question is:

What the heck language is C/C++?

C and C++ are distinct languages that have a similar looking syntax (the same goes for Java, C#, JavaScript, Perl, etc.) And just like the languages I've mentioned in parenthesis, C and C++ also have many syntactic differences. So if they don't have the same syntax, there must be another reason why people conjoin the two languages…

Do they support the same language features?

There is a misconception that every C program is also a C++ program, or that C is a proper subset of C++. This is false. C++ has had a stronger type system than C's, and a concequence of that is certain valid C programs are not valid C++ programs. Case in point:

int *ptr = malloc(sizeof (int) * N);

is valid C, and is quite common, since void pointers can be freely cast to and from other pointer types in C. It would be line noise to write the explicit (int) cast in front of it. However C++ clamps down on this restriction and forbids the implicit casting of void pointers to other pointer types. You need an explicit static_cast to make the code valid C++.

C also supports the ability to allocate arrays on the stack. These were introduced in C99 and have yet to be adopted by the C++ language. (NOTE: There is some work going into bringing a concept similar to C-style stack-allocated arrays to C++, but they will behave differently in order to preserve the semantics of sizeof).

But they're used in the same way, right?

So they don't have the same syntax, and they're not subsets/supersets of one another, what about best practices?

Since C++ supports RAII through constructors and destructors, it has greatly simplified resource handling to the following formula:

  1. Aquire your resource in a constructor
  2. Release your resource in a destructor

The above, combined with allocating your objects on the stack, unless you really need to, results in clean, deterministic resource management. How could we do this in C? The best I can come up with is the following:

  1. Aquire your resource through a function call (passing a pointer to your resource, so that you can indicate failure through the return value.
  2. If the return value is bad, goto cleanup
  3. Use your resource, repeating steps 1 and 2 for any other resources you need.
  4. Clean up your resources at the bottom of the function, lest you leak memory, files, etc.

I'm not criticizing the C style, I'm just showcasing the difference between the two languages. Both styles have benefits, but they sure don't look the same.

Anything else?

I could iterate through several other examples of how C and C++ differ, both at the language level and also with regards to best practices, but I think I've made my point fairly clear. There is no language called C/C++. If anything C and C++ are sibblings, with the parent being K&R C. Both C and C++ have evolved from their common ancestor, and they do occasionally share features, but they are in no way the same language, and should not be grouped together. It makes as much sense to start referring to Java/Ruby since they both inherit features from Smalltalk.