Compatibility of C and C++

Compatibility of C and C++C gradually evolved into C++, although C is still often used. It is possible to mix the two in a single program. This article discusses the compatibility of C and C++. Compatibility of two programming languages refers to the extent to which a program written in one of the languages can be used without modification in the other. Compatibility includes both syntax (grammar) and semantics (the execution of grammatical statements). C and C++ have a degree of upward-compatibility, but there are differences since they are distinct languages that have evolved separately.

Compatibility of C and C++

The C and C++programming languages are closely related. C++ began as a fork of an early, pre-standardized C, and was designed to be source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages (such as IDEs and compilers) are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

However, C is not a subset of C++, and most non-trivial C programs will not compile as C++ code without modification. Likewise, C++ introduces many features that are not available in C and in practice almost all code written in C++ is not conforming C code. This article, however, focuses on differences that cause conforming C code to be ill-formed C++ code, or to be conforming/well-formed in both languages, but to behave differently in C and C++.

Bjarne Stroustrup, the creator of C++, has suggested that the incompatibilities between C and C++ should be reduced as much as possible in order to maximize inter-operability between the two languages. Others have argued that since C and C++ are two different languages, compatibility between them is useful but not vital; according to this camp, efforts to reduce incompatibility should not hinder attempts to improve each language in isolation. The official rationale for the 1999 C standard (C99) "endorse[d] the principle of maintaining the largest common subset" between C and C++ "while maintaining a distinction between them and allowing them to evolve separately", and stated that the authors were "content to let C++ be the big and ambitious language".

Several additions of C99 are or were not supported in C++ or conflicted with C++ features, such as variadic macros, compound literals, designated initializers, variable-length arrays, and native complex number types. The long long int datatype and restrict type qualifier defined in C99 were not included in the C++03 standard, but most mainstream compilers such as the GNU Compiler Collection, Microsoft Visual C++, and Intel C++ Compiler provided similar functionality as an extension. The long long datatype along with variadic macros are present in the subsequent C++ standard, C++11. On the other hand, C99 has reduced some other incompatibilities by incorporating C++ features such as // comments and mixed declarations and code.

Constructs valid in C but not in C++

  • One commonly encountered difference is C being more weakly-typed regarding pointers. For example, C allows a void* pointer to be assigned to any pointer type without a cast, while C++ doesn't; this idiom appears often in C code using malloc memory allocation, or in the passing of context pointers to the pthreads API and other frameworks involving callbacks. For example, the following is valid in C but not C++:
void* ptr;
/* Implicit conversion from void* to int* */
int *i = ptr;
or similarly:
int *j = malloc(sizeof(int) * 5); /* Implicit conversion from void* to int* */
In order to make the code compile as both C and C++, one must use an explicit cast, as follows (with some potentially unpleasant side effects in both languages):
void* ptr;
int *i = (int *)ptr;
int *j = (int *)malloc(sizeof(int) * 5);
  • C++ adds numerous additional keywords to support its new features. This renders C code using those keywords for identifiers invalid in C++. For example:
struct template
 int new;
 struct template* class;
is valid C code, but is rejected by a C++ compiler, since the keywords "template", "new" and "class" are reserved.
  • C++ compilers prohibit goto or switch from crossing an initialization, as in the following C99 code:
void fn(void)
 goto flack;
 int i = 1;
  • In C, struct, union, and enum types must be indicated as such whenever the type is referenced. In C++, all declarations of such types carry the typedef implicitly. As a result, C allows declaring type with the same name as a struct, union or enum.
typedef int BOOL;
  • C allows for multiple tentative definition of a single global variable in a single translation unit.
int N;
int N = 10;
  • Enumeration constants (enum values) are always of type int in C, whereas they are distinct types in C++ and may have a size different from that of int. C++11 allows the programmer to use custom integer types for the values of an enum.
  • C++ changes some C standard library functions to add additional polymorphic functions with const type qualifiers, e.g. strchr returns char* in C, while C++ acts as if there were two polymorphic functions const char *strchr(const char *) and a char *strchr(char *).
  • In both C and C++, one can define nested struct types, but the scope is interpreted differently (in C++, a nested struct is defined only within the scope/namespace of the outer struct).
  • Non-prototype ("K&R"-style) function declarations are not allowed in C++, although they have also been deprecated in C since 1990. Similarly, implicit function declarations (using functions that have not been declared) are not allowed in C++, but have also been deprecated in C since 1999.
  • C allows struct, union, and enum types to be declared in function prototypes, whereas C++ does not.
  • In C, a function prototype without parameters, e.g. int foo();, implies that the parameters are unspecified. Therefore, it is legal to call such a function with one or more arguments, e.g. foo(42, "hello world"). In contrast, in C++ a function prototype without arguments means that the function takes no arguments, and calling such a function with arguments is ill-formed. In C, the correct way to declare a function that takes no arguments is by using 'void', as in int foo(void);, which is also valid in C++.
  • C++ is more strict than C about pointer assignments that discard a const qualifier (e.g. assigning a const int* value to an int* variable): in C++ this is invalid and generates a compiler error (unless an explicit typecast is used), whereas in C this is allowed (although many compilers emit a warning).
  • In C++ a const variable must be initialized; in C this is not necessary.
  • Complex arithmetic using the float complex and double complex primitive data types was added in the C99 standard, via the _Complex keyword and complex convenience macro. In C++, complex arithmetic can be performed using the complex number class, but the two methods are not code-compatible.
  • C99 and C11 added several additional features to C that have not been incorporated into standard C++, such as the restrict keyword, designated initializers, and flexible array members.
  • The last member of a C99 structure type with more than one member may be a "flexible array member", which takes the syntactic form of an array with unspecified length. This serves a purpose similar to variable-length arrays (see below), but VLAs cannot appear in type definitions, and unlike VLAs, flexible array members have no defined size. ISO C++ has no such feature. Example:
struct X
 int n, m;
 char bytes[];
  • Designated initializers for structs and arrays are valid only in C:
struct X a = {.n = 4, .m = 6};
char s[20] = {[0] = 'a', [8]='g'};
  • Array parameter qualifiers in functions.
int foo(int a[const]);
int bar(char s[static 5]);
  • Variable length arrays. This feature leads to possibly non-compile time size of operator.
void foo(size_t x, int a[*]); // VLA declaration
void foo(size_t x, int a[x])
 printf("%zu\n", sizeof a); // same as sizeof(int*)
 char s[x*2];
 printf("%zu\n", sizeof s); // will print x*2

Constructs that behave differently in C and C++

There are a few syntactical constructs that are valid in both C and C++, but produce different results in the two languages.

For example, character literals such as 'a' are of type int in C and of type char in C++, which means that sizeof 'a' will generally give different results in the two languages: in C++, it will be 1, while in C it will be sizeof(int). As another consequence of this type difference, in C, 'a' will always be a signed expression, regardless of whether or not char is a signed or unsigned type, whereas for C++ this is compiler implementation specific.

C++ assigns internal linkage to namespace-scoped constvbariables unless they are explicitly declared extern, unlike C in which extern is the default for all file-scoped entities. 

C distinguishes two kinds of definitions of inline functions: ordinary external definitions (where extern is explicitly used) and inline definitions. C++, on the other hand, provides only inline definitions for inline functions. An inline definition is similar to an internal one, in that it can coexist in the same program with one external definition and any number of internal and inline definitions of the same function in other translation units, all of which can differ. This is a separate consideration from the linkage of the function, but not an independent one. C compilers are afforded the discretion to choose between using inline and external definitions of the same function when both are visible. C++, however, requires that if a function with external linkage is declared inline in any translation unit then it must be so declared (and therefore also defined) in every translation unit where it is used, and that all the definitions of that function be identical.

Several of the other differences from the previous section can also be exploited to create code that compiles in both languages but behaves differently. For example, the following function will return different values in C and C++:

extern int T;
int size(void)
 struct T { int i; int j; };
 return sizeof(T);
 /* C: return sizeof(int)
 * C++: return sizeof(struct T)

This is due to C requiring struct in front of structure tags (and so sizeof(T) refers to the variable), but C++ allowing it to be omitted (and so sizeof(T) refers to the implicit typedef). Beware that the outcome is different when the extern declaration is placed inside the function: then the presence of an identifier with same name in the function scope inhibits the implicit typedef to take effect for C++, and the outcome for C and C++ would be the same. Observe also that the ambiguity in the example above is due to the use of the parenthesis with the sizeof operator. Using sizeof T would expect T to be an expression and not a type, and thus the example would not compile with C++. 

Both C99 and C++ have a boolean type bool with constants true and  false, but they behave differently. In C++, bool is a built-in type and a reserved keyword. In C99, a new keyword,  _Bool, is introduced as the new boolean type. In many aspects, it behaves much like an unsigned int, but conversions from other integer types or pointers always constrained to 0 and 1. Other than for other unsigned types, and as one would expect for a boolean type, such a conversion is 0 if and only if the expression in question evaluates to  0 and it is 1 in all other cases. The header  stdbool.h provides macros booltrue, and false that are defined as _Bool1, and 0, respectively.

Linking C and C++ code

While C and C++ maintain a large degree of source compatibility, the object files their respective compilers produce can have important differences that manifest themselves when intermixing C and C++ code. Notably:

  • C compilers do not name mangle symbols in the way that C++ compilers do.
  • Depending on the compiler and architecture, it also may be the case that calling conventions differ between the two languages.

For these reasons, for C++ code to call a C function foo(), the C++ code must prototype foo() with  extern "C". Likewise, for C code to call a C++ function bar(), the C++ code for bar() must be declared with extern "C".

A common practice for header files to maintain both C and C++ compatibility is to make its declaration be extern "C" for the scope of the header:

/* Header file foo.h */
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
/* These functions get C linkage */
void foo();
struct bar { /* ... */ };
#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */

Differences between C and C++ linkage and calling conventions can also have subtle implications for code that uses function pointers. Some compilers will produce non-working code if a function pointer declared extern "C" points to a C++ function that is not declared extern "C".

For example, the following code:

1 void my_function();
2 extern "C" void foo(void (*fn_ptr)(void));
4 void bar()
5 {
6 foo(my_function);
7 }

Using Sun Microsystems' C++ compiler, this produces the following warning:

 $ CC -c
 "", line 6: Warning (Anachronism): Formal argument fn_ptr of type
 extern "C" void(*)() in call to foo(extern "C" void(*)()) is being passed

This is because my_function() is not declared with C linkage and calling conventions but is being passed to the C function  foo().

Source: Wikipedia,
Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 License.

Last modified: Friday, August 11, 2023, 1:00 PM