Compatibility of C and C++
The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages 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 nontrivial 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 interoperability 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 "endorse
Several additions of C99 are not supported in the current C++ standard or conflicted with C++ features, such as variable-length arrays, native complex number types and the
restrict
type qualifier. On the other hand, C99 reduced some other incompatibilities compared with C89 by incorporating C++ features such as //
comments and mixed declarations and code.Constructs valid in C but not in C++
C++ enforces stricter typing rules, and initialization requirements than C, and so some valid C code is disallowed in C++. A rationale for these is provided in Annex C.1 of the ISO C++ standard.- One commonly encountered difference is C being more weakly-typed regarding pointers. Specifically, C allows a
void*
pointer to be assigned to any pointer type without a cast, while C++ does not; this idiom appears often in C code usingmalloc
memory allocation, or in the passing of context pointers to the POSIX pthreads API, and other frameworks involving callbacks. For example, the following is valid in C but not C++:
/* Implicit conversion from void* to int* */
int *i = ptr;
void *ptr;
int *i = ptr;
int *j = malloc;
Though C++ favors this:
void *ptr;
auto i = reinterpret_cast
auto j = new int;
- C allows implicitly adding additional non-topmost cv-qualifiers where it is safe, while C++ simply gives up.
- C++ changes some C standard library functions to add additional overloaded functions with
const
type qualifiers, e.g.strchr
returnschar*
in C, while C++ acts as if there were two overloaded functionsconst char *strchr
and achar *strchr
. - C++ is also more strict in conversions to enums: ints cannot be implicitly converted to enums as in C. Also, Enumeration constants are always of type
int
in C, whereas they are distinct types in C++ and may have a size different from that ofint
. C++11 allows the programmer to use custom integer types for the values of an enum. - In C++ a
const
variable must be initialized; in C this is not necessary. - C++ compilers prohibit goto or switch from crossing an initialization, as in the following C99 code:
void fn
- While syntactically valid, a
longjmp
results in undefined behaviour in C++ if the jumped-over stack frames include objects with nontrivial destructors. The C++ implementation is free to define the behaviour such that destructors would be called. However, this would preclude some uses of longjmp which would otherwise be valid, such as implementation of threads or coroutines by longjmping between separate call stacks - when jumping from the lower to the upper call stack in global address space, destructors would be called for every object in the lower call stack. No such issue exists in C. - C allows for multiple tentative definitions of a single global variable in a single translation unit, which is disallowed as an ODR violation in C++.
int N;
int N = 10;
- C allows declaring a new type with the same name as an existing
struct
,union
orenum
which is not allowed in C++, as in Cstruct
,union
, andenum
types must be indicated as such whenever the type is referenced whereas in C++ all declarations of such types carry the typedef implicitly.
enum BOOL ;
typedef int BOOL;
- Non-prototype function declarations are not allowed in C++; they are still allowed in C, although they have been deemed obsolescent since C's original standardization in 1990. Similarly, implicit function declarations are not allowed in C++, and have been disallowed in C since 1999.
- 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
. 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 inint foo;
, which is also valid in C++. Empty function prototypes are a deprecated feature in C99. - In both C and C++, one can define nested
struct
types, but the scope is interpreted differently: in C++, a nestedstruct
is defined only within the scope/namespace of the outerstruct
, whereas in C the inner struct is also defined outside the outer struct.
- Complex arithmetic using the
float complex
anddouble complex
primitive data types was added in the C99 standard, via the_Complex
keyword andcomplex
convenience macro. In C++, complex arithmetic can be performed using the complex number class, but the two methods are not code-compatible. - Variable length arrays. This feature leads to possibly non-compile time operator.
void foo; // VLA declaration
void foo
- 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, 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
- The
restrict
type qualifier defined in C99 was not included in the C++03 standard, but most mainstream compilers such as the GNU Compiler Collection, Microsoft Visual C++, and Intel C++ Compiler provide similar functionality as an extension. - Array parameter qualifiers in functions are supported in C but not C++.
int foo; // equivalent to int *const a
int bar; // annotates that s is at least 5 chars long
- The functionality of compound literals in C is generalized to both built-in and user-defined types by the list initialization syntax of C++11, although with some syntactic and semantic differences.
struct X a = ; // The equivalent in C++ would be X. The C syntactic form used in C99 is supported as an extension in the GCC and Clang C++ compilers.
- Designated initializers for structs and arrays are valid only in C, although struct designated initializers are planned for addition in C++2x:
struct X a = ; // to be allowed in C++2x
char s = ; // allowed in C, not allowed in C++
- Functions that do not return can be annotated using a noreturn attribute in C++ whereas C uses a distinct keyword.
- C allows
struct
,union
, andenum
types to be declared in function prototypes, whereas C++ does not.
struct template
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.- Character literals such as
'a'
are of typeint
in C and of typechar
in C++, which means thatsizeof 'a'
will generally give different results in the two languages: in C++, it will be1
, while in C it will besizeof
. As another consequence of this type difference, in C,'a'
will always be a signed expression, regardless of whether or notchar
is a signed or unsigned type, whereas for C++ this is compiler implementation specific. - C++ assigns internal linkage to namespace-scoped
const
variables unless they are explicitly declaredextern
, unlike C in whichextern
is the default for all file-scoped entities. Note that in practice this does not lead to silent semantic changes between identical C and C++ code but instead will lead to a compile-time or linkage error. - In C use of inline functions requires manually adding a prototype declaration of the function using the extern keyword in exactly one translation unit to ensure a non-inlined version is linked in, whereas C++ handles this automatically. In more detail, C distinguishes two kinds of definitions of
inline
functions: ordinary external definitions and inline definitions. C++, on the other hand, provides only inline definitions for inline functions. In C, 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 in every translation unit where it is used, and that all the definitions of that function be identical, following the ODR. Note that static inline functions behave identically in C and C++. - Both C99 and C++ have a boolean type
bool
with constantstrue
andfalse
, but they are defined 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. The headerstdbool.h
provides macrosbool
,true
andfalse
that are defined as_Bool
,1
and0
, respectively. Therefore,true
andfalse
have typeint
in C.
extern int T;
int size
This is due to C requiring
struct
in front of structure tags, but C++ allowing it to be omitted. 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++.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.
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 */
- endif
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:
void my_function;
extern "C" void foo);
void bar
Using Sun Microsystems' C++ compiler, this produces the following warning:
$ CC -c test.cc
"test.cc", line 6: Warning : Formal argument fn_ptr of type
extern "C" void in call to foo) is being passed
void.
This is because
my_function
is not declared with C linkage and calling conventions, but is being passed to the C function foo
.