Skip to the content.

G+: C and C++ are almost interchangeable

David Coles
C and C++ are almost interchangeable. Except when they're not. You'd also think it would take less than 10 years to incorporate some of these features into the C++ standard.

C is not a subset of C++ | David's Software Blog


(+1's) 3
Abdulla Kamar
Initialiser lists aren't a superficial change, and there was a freeze period put on major changes to C++ for compilers to catch up. (Which they later realised was a mistake, and now require implementations of features before they will consider them.)

It goes both ways, it took C a long time to pick up features from C++ and sometimes they've decided not to. Also, some things just don't make sense for C++ to adopt, like VLAs or complex numbers, due to better C++ equivalents.

Matt Giuca
+Abdulla Kamar I disagree with your second paragraph.

"It goes both ways, it took C a long time to pick up features from C++ and sometimes they've decided not to."
This isn't a valid defence of C++, since nobody has ever had the remotest possibility of porting C++ code to C. Of course very few C++ features have been picked up by C. On the other hand, C++ is supposed to be a near-superset of C. Ideally, you should be able to take C code and run it as C++. Obviously there will always be trouble if you have variables called 'class' or 'new', but other than that, it should work. That is the whole strength of C++ is its immense level of backwards compatibility with C.

"Also, some things just don't make sense for C++ to adopt, like VLAs or complex numbers, due to better C++ equivalents."
That's besides the point. Since C++ should be a superset of C. Support VLAs and complex numbers, and provide a better equivalent. Doesn't make sense for Java, but it makes sense for C++ which would ensure C code is forwards compatible with C++. By this logic, you could say "C++ shouldn't have POD types since it has classes" or "C++ shouldn't have function pointers since it has std::function."

I realise that you could counter both of these arguments by saying "but C++ is not supposed to be a superset of C, you're just imposing that on the language -- it is a language in its own right." But I don't buy that. A very big reason for C++'s success is that people were able to take C programs and C++ify them. (For instance, the fact that there is no need to write C++ bindings for C libraries unless you really want to C++ify them -- calling them just works.)

Abdulla Kamar
C++ 98 was meant to be a superset of ANSI C --- since then they have diverged. There have been attempts at unifying parts, but fundamentally they're incompatible now. So yes, that is the state of things now, and most C99-only features aren't exposed at an API level.

David Coles
I'm worried that it's only going to get worse. There's a good justification for writing libraries in C since it's often considered the lowest common denominator. And C++ being able to "just work" with C is a huge feature. But things are certainly becoming muddier especially when people start writing APIs that use C99-only features (see http://ffmpeg.org/pipermail/ffmpeg-devel/2010-May/095488.html for a recent example).

I'm a huge fan of <stdint.h> and think it's quite sensible to use it in public headers, but if it means you can't compile the code with a C++ compiler... Really I don't want to get to a point where we have to write explicit bindings for C++ just to use a C library.

Abdulla Kamar
That seems to be more an issue with a version of the header. There's even a <cstdint>, so it's not fundamentally incompatible.

I do agree though, incompatibilities are a worry, but so far they haven't proven too much of an issue.

Reuben Bond
C++11 takes steps to converge on compatibility. There is also C11, now. Hopefully it improves compatibility further.

Matt Giuca
+Abdulla Kamar That is what happened, but it isn't good. I think we were arguing that C++ should be keeping up to date with C features. I don't see why they're "fundamentally incompatible" -- just that they simply didn't enable those features.

+David Coles At least it should be the case that you can call C code from C++ -- as long as you compile the C99 code with a C compiler and the C++ code with a C++ compiler, they should still be able to call each other as long as the headers use extern "C" and don't use any C99 features in the interface.