C is not a subset of C++

When I tell people that I program in C, they automatically assume that I’ve been living underneath a rock, hiding out in my parent’s basement, and wasting away without ever seeing the sun.

They say “Just use C++! It has all the features of C and more!” But C++ and C are not compatible.

In mathematics, set A is a subset of set B if and only if all elements of A are also elements of B. It can be written as “x ∈ A ⇒ x ∈ B.” If you have never seen this syntax before, essentially it reads “x is an element of A implies that x is also an element of B”. This means that everything in A by definition must also be in B.

This is not the case with C and C++. In fact, I think the name “C++” is actually quite deceptive. Bjarne Stroustrup’s original implementation of C++ was actually called “C with classes,” which even more so gives the idea that it is an extension to C.

In fact, C++ is not an extension to C. It is a completely separate language, designed by a completely separate committee, with completely separate design goals. In order for C++ to be an extension to C, all valid C programs must also be valid C++ programs, and they must produce the same output.

One example of C++ diverging from C and becoming incompatible is with pointer conversion. In C, a void pointer can be implicitly converted to a pointer of a different type. In C++, this is not the case!

Consider the following C program:

void PointerConversion(void)
{
 void *ptr;
 int *i = ptr;
}

This code compiles in C, but does not compile in C++!

There’s a popular programmer joke about the name “C++.” In both C and C++, the postfix ”++” operator increments a variable and returns the value before the increment (e.g. x++ evaluates to x and then sets x to x + 1). So, in the case of “C++”, if you evaluate C++ as an expression, you will actually get C, which is not the case as demonstrated above!

Read more: Compatibility of C and C++