promotion image of download ymail app

電腦的程式語言 C 同 C++ 有何分別

電腦的程式語言 C 同 C++ 有何分別?


1 個解答

  • 阿一
    Lv 7
    1 十年前

    The C++ programming language was derived from C. It adds object-oriented functionality with C-like syntax. C++ adds greater typing strength, scoping and other tools useful in object-oriented programming and permits generic programming via templates. Nearly a superset of C, C++ now supports most of C, with a few relevant exceptions (mostly of stronger typing restriction).

    The C and C++ programming languages are closely related. C++ grew out of C and is mostly a superset of the latter. 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.

    C++ introduces many features that are not available in C — C++ code is not valid C code. Here, however, we focus on differences that cause valid C code to be invalid C++ code, or to be valid in both languages but to behave differently in C and 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 legal C code, but is rejected by a C++ compiler, since the keywords "template", "new" and "class" are reserved.

    C++ compilers prohibit goto from crossing an initialization, as in the following C99 code:

    void fn(void){

    goto flack;

    int i = 1;



    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') gives different results in the two languages.

    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 implicit typedefs created by C++ struct declarations can change the meaning of an identifier in a given scope.

    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 C, these keywords and types are only defined if the program includes the stdbool.h header, and may be redefined by the programmer.


    There are many other C syntaxes which are invalid or behave differently in C++, see here

    • Commenter avatar登入以回覆解答