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, called "C with Classes", 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 (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 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 (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 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, C has gradually worked to reduce some other incompatibilities by incorporating C++ features such as single line comments with // and mixed declarations and code in C99, and adding attributes, type deduction with the auto keyword, and a nullptr constant in C23.

Constructs valid in C but not in C++

C++ enforces stricter typing rules (no implicit violations of the static type system), and initialization requirements (compile-time enforcement that in-scope variables do not have initialization subverted) than C, and so some valid C code is invalid in C++. A rationale for these is provided in Annex C.1 of the ISO C++ standard.

C99 and C11 added several additional features to C that have not been incorporated into standard C++ as of C++20, such as complex numbers, variable length arrays (complex numbers and variable length arrays are designated as optional extensions in C11), flexible array members, the restrict keyword, array parameter qualifiers, and compound literals.

Constructs that behave differently in C and C++

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

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 { long i, 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++.

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:

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:

In foo.h:

// If this is a C++ compiler, use C linkage 
#ifdef __cplusplus
extern "C" {
#endif

// Any symbol declared here gets C linkage
void foo();
int bar(int a, int b);

// If this is a C++ compiler, end C linkage
#ifdef __cplusplus
}
#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 qux();
extern "C" void foo(void (*fp)(void));

void bar() {
   foo(qux);
}

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

 "test.cpp", line 5: Warning (Anachronism): Formal argument fp of type
 extern "C" void(*)() in call to foo(extern "C" void(*)()) is being passed
 void(*)().

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

See also

References

  1. 1 2 Stroustrup, Bjarne. "An Overview of the C++ Programming Language in The Handbook of Object Technology (Editor: Saba Zamir). CRC Press LLC, Boca Raton. 1999. ISBN 0-8493-3135-8" (PDF). p. 4. Archived (PDF) from the original on 16 August 2012. Retrieved 12 August 2009.
  2. ↑ B.Stroustrup. "C and C++: Siblings. The C/C++ Users Journal. July 2002" (PDF). Retrieved 17 March 2019.
  3. ↑ "Bjarne Stroustrup's FAQ – Is C a subset of C++?". Retrieved 22 September 2019.
  4. ↑ B. Stroustrup. "C and C++: A Case for Compatibility. The C/C++ Users Journal. August 2002" (PDF). Archived (PDF) from the original on 22 July 2012. Retrieved 18 August 2013.
  5. ↑ Rationale for International Standard—Programming Languages—C Archived 6 June 2016 at the Wayback Machine, revision 5.10 (April 2003).
  6. ↑ "C Dialect Options - Using the GNU Compiler Collection (GCC)". gnu.org. Archived from the original on 26 March 2014.
  7. ↑ "WG14-N2335: Attributes in C" (PDF). open-std.org. 9 March 2019. Archived (PDF) from the original on 26 October 2022.
  8. ↑ "WG14-N3007: Type inference for object definitions". open-std.org. 10 June 2022. Archived from the original on 24 December 2022.
  9. ↑ "WR14-N3042: Introduce the nullptr constant". open-std.org. 22 July 2022. Archived from the original on 24 December 2022.
  10. ↑ "N4659: Working Draft, Standard for Programming Language C++" (PDF). §Annex C.1. Archived (PDF) from the original on 7 December 2017. ("It is invalid to jump past a declaration with explicit or implicit initializer (except across entire block not entered). … With this simple compile-time rule, C++ assures that if an initialized variable is in scope, then it has assuredly been initialized.")
  11. ↑ "N4659: Working Draft, Standard for Programming Language C++" (PDF). §Annex C.1. Archived (PDF) from the original on 7 December 2017.
  12. ↑ "IBM Knowledge Center". ibm.com.
  13. ↑ "FAQ > Casting malloc - Cprogramming.com". faq.cprogramming.com. Archived from the original on 5 April 2007.
  14. ↑ "Qualifier-preserving standard library functions, v4" (PDF).
  15. ↑ "longjmp - C++ Reference". www.cplusplus.com. Archived from the original on 19 May 2018.
  16. ↑ "WG14 N2432 : Remove support for function definitions with identifier lists" (PDF).
  17. ↑ "2011 ISO C draft standard" (PDF).
  18. ↑ "WG14 N 2841: No function declarators without prototypes".
  19. ↑ "std::complex - cppreference.com". en.cppreference.com. Archived from the original on 15 July 2017.
  20. ↑ "Incompatibilities Between ISO C and ISO C++". Archived from the original on 9 April 2006.
  21. ↑ Restricted Pointers Archived 6 August 2016 at the Wayback Machine from Using the GNU Compiler Collection (GCC)
  22. ↑ "WG14-N2764 : The noreturn attribute" (PDF). open-std.org. 21 June 2021. Archived (PDF) from the original on 25 December 2022.
  23. ↑ "Information technology — Programming languages — C" (PDF). open-std.org. 4 May 2025.
  24. ↑ Jan Schultke (10 May 2026). "break label; and continue label;". open-std.org. WG21.
  25. ↑ Jan Schultke (21 September 2026). "Case ranges". open-std.org. WG21.
  26. ↑ "IBM Knowledge Center". ibm.com.
  27. ↑ "IBM Knowledge Center". ibm.com.
  28. ↑ "Oracle Documentation". Docs.sun.com. Archived from the original on 3 April 2009. Retrieved 18 August 2013.