Beruflich Dokumente
Kultur Dokumente
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;
Compatibility of C and C++ 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).
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.
References
[1] Stroustrup, Bjarne. "An Overview of the C++ Programming Language" (http:/ / www. research. att. com/ ~bs/ crc. pdf) (PDF). p. 4. . Retrieved 2009-08-12. [2] http:/ / www. research. att. com/ ~bs/ sibling_rivalry. pdf [3] Rationale for International StandardProgramming LanguagesC (http:/ / www. open-std. org/ jtc1/ sc22/ wg14/ www/ C99RationaleV5. 10. pdf), revision 5.10 (April 2003). [4] Restricted Pointers (http:/ / gcc. gnu. org/ onlinedocs/ gcc/ Restricted-Pointers. html#Restricted-Pointers) from Using the GNU Compiler Collection (GCC) [5] http:/ / david. tribble. com/ text/ cdiffs. htm [6] http:/ / www. parashift. com/ c+ + -faq-lite/ const-correctness. html#faq-18. 17 [7] http:/ / docs. sun. com/ source/ 819-3689/ Ch3. Std. html#pgfId-18503
Detailed comparison (http://www.coding-guidelines.com/cbook/c90c++.pdf) , sentence by sentence, from a C Standard perspective. Incompatibilities Between ISO C and ISO C++ (http://david.tribble.com/text/cdiffs.htm), David R. Tribble (Aug 2001). Sun Microsystems C++ Migration Guide, section 3.11 (http://docs.sun.com/source/819-3689/Ch3.Std. html#pgfId-18154), documentation on linkage issues for the Sun C++ compiler.
License
Creative Commons Attribution-Share Alike 3.0 Unported http:/ / creativecommons. org/ licenses/ by-sa/ 3. 0/