Friday, April 15, 2011

C++ is not C

C++ compilers (g++ or visual c++ for example) can compile many C codes, but not necessarily all of them. Here is a simple but lame example:

int class, template;

Above code compiles fine with a C code but not with C++, as 'class' and 'template' are keywords in C++. But again, it's really a lame example.

A good example is the feature set of C99. Here are some codes that are valid in C but not in C++:

int vec[5] = { [1]=10, [3]=20 }; // designated initializers

typedef struct
{
char name[20];
int ID;
int age;
}Employee;

Employee emp = {.ID = 0, .age = 0};

int main()
{
}

The reason for not having the support for the C99 code in some major C++ compilers is that C++ was standardized in 98 and C99 standards came after that.

Will try to post more on this later.

No comments: