Home > Blogs > 30 C++ Tips in 30 Days: Tip # 24 coping with incompatibilites between C99 and C++

30 C++ Tips in 30 Days: Tip # 24 coping with incompatibilites between C99 and C++

By  Dec 15, 2007

Topics: Programming, C/C++

C99 features such as restrict pointers, designated initializers and variable length arrays haven't been accepted into the C++ standard. Sadly, at this stage I can already tell you that C++09 will not support these features either. The incompatibility between the two languages should concern your if you intend to import a C source file into a C++ project. There are however workarounds.

There's no point in apportioning the blame for the current grim state of affairs: for nearly 10 years, standard core C features aren't supported in C++, and that chasm isn't going to be fixed in the next revision of C++ known as C++09. What can you then when you migrate from C to C++ or wish to import a C source file into a C++ project?

The first option is to stick right from the start to C94 or C89 exclusively in new C projects and avoid any C99 features. This will ensure that C code will compiler as C++ code without any problems. However, this approach isn't practical if you need to import a large amount of C99 code into C++, or when you truly need C99 features.

Here's the secret: although C++ doesn't officially support many of the C99 features, many compilers have a C99 flag which you can set when compiling C++ code. This flag forces the C++ compiler to accept C99 features, treating them as non-standard C++ extensions. Notice that the C99 flag is switched off by default so you need to set it explicitly.

Try to look for this hidden flag when you import C99 code into C++. At the moment, it's the most cost effective method of dealing with the discrepancies between the two languages.