The comment to this answer got me wondering. I've always thought that C was a proper subset of C++, that is, any valid C code is valid C++ code by extension. Am I wrong about that? Is it possible to write a valid C program that is not valid C++ code?

EDIT: This is really similar to, but not an exact duplicate of this question.

Technically they compile together (as noted above). But writting C code in a C++ file (aka C with classes) does not mean it is C++. The styles for each language is just so radically different, moving from C to C++ is like learning a new language. Moving from C++ to C is just too painful.
–
Loki AstariNov 20 '08 at 2:46

Also note that C99 adds several features which aren't permitted in C++ (or are only supported using vendor extensions), such as builtin _Complex and _Imaginary data types, variable-length arrays (arrays sized at runtime rather than compile time), flexible array members (arrays declared as the last member of a struct that may contain an unspecified number of elements), and more.

Interesting, I didn't know about all 3 of those. Do you have a reference for C++ not allowing recursive calls to main? g++ will only complain about it if I add the -pedantic flag.
–
Adam RosenfieldNov 20 '08 at 5:28

I can't find a reference to it, I just remember this came up a lot in comp.lang.c when people came looking for help with C/C++, which caused the regulars to flip out and yell "They are separate languages!"
–
FigBugNov 20 '08 at 21:33

1

Note that // is permitted in C99 - which is the current C standard.
–
Jonathan LefflerJan 23 '09 at 8:07

One thing that sets the two apart and comes up in day to day development is linkage and function name mangling. A C function compiled with a C compiler is not accessible to C++ unless the prototype is marked up with extern "C".

I'm pretty sure your first example is wrong. Comeau rejects it. If you provide a parameter list, then you have to honor it. If you leave the parameter list blank, then you can pass any parameters you want.
–
Rob KennedyNov 20 '08 at 5:36

This is valid C code which does not compile in most C++ compilers. It is not part of the C++ spec as far as I know. However, some C++ compilers are "liberal" with certain parts of the language and allow things they shouldn't, just as a lot miss out on a few nuances that are in the spec but almost never used.