It's now sort of you CAN write C++ compliant C code, but there's things that you can do by default in C (such as implicitly converting from void pointers) that C++ doesn't like you doing without an explicit cast
Just stay within the subset of c++ and c that's valid in both, then give your file names .cpp extensions, and pass cpp specific arguments to your build system, but again.... why?
C is mostly compilable in c++ compilers. I think the only incompability is that c++ has more key words than c, so if e.g. a variable in c is called something that is a keyword in c++, then it will fail.
Yup. VLAs, while much maligned in C, are technically part of the C standard. They are not part of the C++ standard (though many compilers support them as extensions). The restrict keyword is another one that C supports but C++ does not. C++ is also generally much stricter about how types and casting are handled, handles inline functions a bit differently, etc.
VLA are not maligned but misunderstood. Probably the most misunderstood concept in C, where many still perceive it as just a syntactic sugar for risky stack allocations, a deeply flawed view. Newer C standard tried to brings them back into the spotlight.
I would not say that C++ makes cast stricter, C style casts are perfectly valid in C++. It just adds more ways to cast things giving a bit more way to express the intensions (i.e. const_cast). A huge misstep is disallowing implicit cast from void* which actually is one of the safer and more convenient implicit cast. Forcing such cast can hide a real defect and this practice is strongly discouraged in C coding guidelines in favour of other safer idioms.
It is worth to mention compound literals that don't exist in C++, and designated initializer which subset was partially adopted by C++.
Other subtle difference is changing type of some expressions where C traditionally used int. The sizeof 'A' is probably the most known example.
73
u/FishWash 2d ago
No no unity is C#
57
u/hpyfox 2d ago
They said C++ was "suppose" to be backwards-compatible/superset of C, but then type-handling walks into the room.
Other problems exist with C and C++ compatibility but that's one of them.
35
u/Legal-Software 2d ago •
Says no one who has ever compared CPU and memory load of the two side-by-side.
6
u/satansprinter 1d ago
I just want c with namespaces and structs with methods
4
u/Percolator2020 1d ago
I mean that’s how it was in the beginning and for a very long time, especially in embedded.
3
u/JackNotOLantern 1d ago
C is mostly compilable in c++ compilers. I think the only incompability is that c++ has more key words than c, so if e.g. a variable in c is called something that is a keyword in c++, then it will fail.
2
u/stupled 1d ago
Is all C underwraps
2
u/dumael 1d ago
One has abstract base virtual private destructors, the other doesn't.
1
u/krak_1 23h ago
Laughs in embedded C impossible to pronounce compiler name.
0
u/Chingiz11 1d ago
TCC would beg to differ