K
Keith Thompson
Well written C compiles with C++. That is a fact.
It may be the case that it's correct C but not correct C++, and vice
versa.
Here's one of the best pages I've found on the net about C/C++
differences:
<http://david.tribble.com/text/cdiffs.htm>
Here's some code that doesn't work in C++, but does work in C,
#include <limits.h>
int main(void) {
char m[sizeof 'A'];
return m[CHAR_BIT == 8] = 0;
}
This takes advantage of the following difference, that 'A' in C has
type int, but in C++ has type char.
In C this code always works; in C++ it never works.
Incorrect. If sizeof(int)==1 (which implies CHAR_BIT>=16), then it
can work in both C and C++.
Sure, there are plenty of contrived examples of code that works in C
but not in C++. Here's another one:
int main(void)
{
#ifdef __cplusplus
#error
#endif
return 0;
}
Such examples are irrelevant to Nick's claim that "Well written C
compiles with C++.", unless you want to claim that either of the above
examples is well written.
Nick's claim is incorrect because well written C does not cast the
result of malloc (and calloc, and realloc). Avoiding that issue
requires writing code that's less than ideal in C for the sake of C++
compatibility. (Avoiding C++ keywords is less of an issue, since well
written C code doesn't *need* to use them as identifiers.)