PeterH:
PaulS:
If you follow recommended practices in C, the code will compile, link, and run the same using a C compiler or a C++ compiler.
It's possible to write 'C' code which will compile and run as C++ code, but I think you're understating the differences between them. You seem to be implying that the potential incompatibilities can be ignored, and I don't think that's right.
PeterH is right. The things he mentioned are reasonably common in C, for the most part.
For example:
In 'C' you can implicitly cast a void pointer to any other type of pointer - in C++ you can't.
How many times have I seen:
int* ptr = malloc(sizeof(int) * 5);
It's not hard to change it to the following:
int* ptr = (int*) malloc(sizeof(int) * 5);
But it does require a change to the code, so off the bat, it wouldn't compile.
But let's say you do write your code so that it's C++ compatible, and it does compile; there are still differences between the two languages which can lead to logical errors. For example, one thing which might trip you up is that character literals in C are actually ints. In C++, they're chars. It's a subtle difference, but in the right situation, it could be important, because they'd have different sizes. You just have to be careful when you're porting something, that's all we're saying Paul.
Let's put it this way: While I recognize the OP is a notable exception to this argument, few people here are going to write C code which is written explicitly to be compatible with C++. Why? Because if they wanted that, they'd probably just actually use C++, and just avoid using features like classes. What would be stopping them? It compiles to machine code, right? So the main reason to have this discussion is if you're porting older libraries from C to C++, and if that's the case, then I can all but guarantee that without much looking you'll quickly run into many of the issues that PeterH has discussed in his previous posts here. I've seen people bypass variable initialization using gotos and everything in certain pieces of code, which (thankfully) will not compile at all in C++.
However, if your intention is to write C code that is compatible with C++, then it is certainly not too difficult to do, Just be aware of the differences, that's all.