Compatibility of C and C++

Compatibility of C and C++
Programming language comparisons
General comparison
Basic syntax
Basic instructions
Arrays
Associative arrays
String operations
String functions
List comprehension
Object-oriented programming
Object-oriented constructors
Database access
Database RDBMS

Evaluation strategy
List of "hello world" programs

ALGOL 58's influence on ALGOL 60
ALGOL 60: Comparisons with other languages
Comparison of ALGOL 68 and C++
ALGOL 68: Comparisons with other languages
Compatibility of C and C++
Comparison of Pascal and Borland Delphi
Comparison of Object Pascal and C
Comparison of Pascal and C
Comparison of Java and C++
Comparison of C# and Java
Comparison of C# and Visual Basic .NET
This box: view · talk · edit

The C and C++ programming languages are closely related. C++ grew out of C, as it was designed to be source-and-link compatible with C.[1] Due to this, C code is often developed with C++ IDEs, integrated with C++ code, and compiled in C++ compilers. While most C source code will compile as C++ code without any changes, certain language differences prevent C++ from being a strict superset of C.

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 [2] 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."[3]

Several additions of C99 are not supported in C++ or conflict 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 qualifier defined in C99 are not included in the current C++ standard, but some compilers such as the GNU Compiler Collection[4] provide them as an extension. The long long datatype along with variadic templates, with which some functionality of variadic macros can be achieved, are present in the new 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.

Contents

Constructs valid in C but not C++

One commonly encountered difference is that C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; this idiom appears often in C code using malloc memory allocation. For example, the following is valid in C but not C++:

void* ptr;
int *i = ptr;       /* Implicit conversion from void* to int* */

or similarly:

int *j = malloc(sizeof(int) * 5);     /* Implicit conversion from void* to int* */

In order to make the code compile in both C and C++, one must use an explicit cast:

void* ptr;
int *i = (int *) ptr;
int *j = (int *) malloc(sizeof(int) * 5);

Another portability issue from C to C++ are the numerous additional keywords that C++ introduced. This makes C code that uses them as 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 using goto or switch from crossing an initialization, as in the following C99 code:

void fn(void)
{
  goto flack;
  int i = 1;
flack:
  ;
}

There are many other C syntaxes which are invalid or behave differently in C++:[5]

  • The comma operator can result in an "l-value" (a quantity that can be used for the left-hand side of an assignment) in C++, but not in C.
  • C does not allow a given typedef to be duplicated in the same scope, whereas C++ allows repeated typedefs.
  • Enumeration constants (enum values) are always of type int in C, whereas they are distinct types in C++ and may have size different from that of int.
  • C++ identifiers are not allowed to contain two or more consecutive underscores in any position. C identifiers are not allowed to start with two or more consecutive underscores, but may contain them in other positions.
  • C++ also changes some C standard library functions to add additional const qualifiers, e.g. strchr returns char* in C and const char* in C++.
  • 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.
  • A struct, union, or enum declaration in C++ usually implies an implicit typedef of the same name, while in C it does not.
  • In C, a function prototype without arguments, 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);.
  • 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),[6] whereas in C this is allowed (although many compilers emit a warning).

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 in C it will be sizeof(int) which on architectures with 8 bit wide char will be at least 2. 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.

The static keyword is used in C to restrict a function or global variable to file scope (internal linkage). This is also valid in C++, although C++ deprecates this usage in favor of anonymous namespaces (which are not available in C). Also, C++ implicitly treats any const global as file scope unless it is explicitly declared extern, unlike C in which extern is the default. Conversely, inline functions in C are of file scope whereas they have external linkage by default in C++.

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 bool, true and false that are defined as _Bool, 1 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" {
#endif
 
/* These functions get C linkage */
void foo();
 
struct bar { /* ... */ };
 
#ifdef __cplusplus /* If this is a C++ compiler, end 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".[7]

For example, the following code:

void my_function();
extern "C" void foo(void (*fn_ptr)(void));
 
void bar()
{
   foo(my_function);
}

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

$ CC -c test.cc
"test.cc", line 6: Warning (Anachronism): Formal argument fn_ptr of type
extern "C" void(*)() in call to foo(extern "C" void(*)()) 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().

References

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Compatibility mode (browser) — Compatibility mode is a feature of Internet Explorer (IE), a web browser, from version 8 onwards. IE8 Internet Explorer 8 was promoted by Microsoft as having stricter adherence to W3C described web standards than Internet Explorer 7. As a result …   Wikipedia

  • Compatibility (mechanics) — Continuum mechanics …   Wikipedia

  • Compatibility layer — A compatibility layer is a term that refers to components that allow for non native support of components. In software engineering, a compatibility layer allows binaries for a legacy or foreign system to run on a host system. This translates… …   Wikipedia

  • Compatibility (geochemistry) — In geochemistry, compatibility is a measure of how readily a particular trace element substitutes for a major element within a mineral. Compatibility of an ion is controlled by two things: its valence and its ionic radius. Both must approximate… …   Wikipedia

  • Compatibility testing — Software Testing portal Compatibility testing, part of software non functional tests, is testing conducted on the application to evaluate the application s compatibility with the computing environment. Computing environment may contain some or… …   Wikipedia

  • Compatibility (chemical) — Chemical compatibility is a measure of how stable a substance is when mixed with another substance[1]. If substances mix and do not change they are considered compatible. If substances mix and change or do not mix at all they are considered… …   Wikipedia

  • Compatibility mode (operating system) — A compatibility mode is a software mechanism in which a computer s operating system emulates an older processor, operating system, and/or hardware platform in order to allow obsolete software to remain compatible with the computer s newer… …   Wikipedia

  • Compatibility card — A compatibility card is an expansion card for computers that allows it to have hardware emulation with another device. The most popular of these were for Macintosh systems that allowed them to emulate Windows PCs via NuBus[1] or PCI. Apple… …   Wikipedia

  • compatibility — noun a) the capability of two or more items or components of equipment or material to exist or function in the same system or environment without mutual interference. b) the ability to execute a given program on different types of computers… …   Wiktionary

  • compatibility — com|pat|i|bil|i|ty [ kəm,pætə bıləti ] noun uncount 1. ) the fact that different ideas or systems can exist together: compatibility with: Christianity s compatibility with science 2. ) COMPUTING the fact that different pieces of computer… …   Usage of the words and phrases in modern English

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”