Is C code still considered C++?

前端 未结 8 1157
小鲜肉
小鲜肉 2021-01-11 22:52

The comment to this answer got me wondering. I\'ve always thought that C was a proper subset of C++, that is, any valid C code is valid C++ code by extension. Am I wrong a

8条回答
  •  天涯浪人
    2021-01-11 23:42

    A few more:

    C allows recursive calls to main, C++ does not

    char foo[3] = "abc" is legal C, not C++

    sizeof('A') == sizeof(int) is true in C and false in C++

    There are even more changes with C99

    Edit: I found a post that lists the majority of the differences. http://c-faq.com/misc/cplusplus.nr.html

    Summary of why C is not proper subset of C++:

    1. auto conversion from void * to any object/incomplete type
    2. new keywords (a number of these)
    3. due to structs becoming scoped
    4. due to struct tags becoming typedefs
    5. due to prototypes being required
    6. implicit int rules
    7. Recursive call to main
    8. Due to // comments
    9. due to character literals being type char and not int etc.

提交回复
热议问题