Actually, I've done C/C++ for years. You may have a little miss
understanding here.
C++ is everything C can do with added fluff, which is why they gave
it the "++" extension in the first place.
No, it is not. C has many features that render it incompatible with C,
and not merely an extension of it. See Annex C of the the C++ standard
for a list. In the meantime, C has gone it's own way, adding many
features in C99 that are incompatible with C++ (while adding a few
features that make it more compatible).
C++ is an extension to C language, which is why you see the C/C++ all
over the place. They are not two different languages. C++ has the Class
model and a few other things that stands above C but it can also do what
C does.
I wouldn't use a C++ compiler that refused to support the basic
operations of C language..
The following is very carefully designed to make many different points
in a program that is as small as possible. I make no claim that it is an
example of good programming practice
It is syntactically valid code in both C and C++, conforms strictly to
the C99 standard, and is well-formed code according to both the C++98
and C++03 standards. It's behavior under C90 is technically undefined,
but only by reason of it's use of __cplusplus, an identifier which a C90
compiler could, in principle, have reserved for it's own incompatible
usage - but such compilers are rare, and probably non-existent.
You can compile and link both modules as C code, or as C++ code; the
resulting executables are guaranteed by the applicable standards to exit
with an failure status, for two entirely different sets of reasons,
depending upon which language is used (note that many compilers
automatically infer the language to be used from the extension on the
filename - you might need to rename the files to get them to actually
compile in one language rather than the other).
If you compile the first module with C, and the second with C++, it is
guaranteed to return a successful exit status. If C++ were really just
an extension to C, then what I've said about how this program's behavior
varies with the programming language would be impossible.
shared.h:
=========
#ifndef SHARED_H
#define SHARED_H
extern char tag;
extern int enumer[2];
typedef void voidvoid(void);
int Cnmtyp(void);
int Cfunc(voidvoid*);
#endif
First module:
=============
#ifdef __cplusplus
extern "C" {
#endif
#include "shared.h"
char tag = 0;
static int hidden(voidvoid *pfunc)
{
(*pfunc)();
tag = sizeof 'C' == sizeof(int);
return Cnmtyp() && enumer[0] && enumer[1];
}
int Cfunc(voidvoid* pfunc)
{
struct tag
{
enum { enumer, other } in;
int integer;
} out;
out.integer = sizeof(tag) == 1 && sizeof(enumer) == sizeof out.in;
return hidden(pfunc) && out.integer;
}
#ifdef __cplusplus
}
#endif
Second module:
==============
#ifdef __cplusplus
extern "C" {
#endif
#include "shared.h"
int enumer[2] = {0, 1};
static void Cppname_Cpptype(void)
{
enumer[0] = sizeof 'C' == 1;
return;
}
#ifdef __cplusplus
}
#endif
int Cnmtyp(void)
{
struct tag
{
enum { enumer, other } in;
int integer;
} out;
out.integer = sizeof(enumer) == 2 * sizeof(int);
return out.integer && sizeof(tag) == sizeof out;
}
static voidvoid Cppname_Ctype;
static void Cppname_Ctype(void) {
Cppname_Cpptype();
}
int main(void) {
return Cfunc(&Cppname_Ctype) && tag;
}
As an exercise for the student: explain precisely why three different
conditional expressions in the above code are guaranteed to have
different values in C and C++, and why two other conditionals will have
different values except in the unlikely case that sizeof(int)==1.