op ol
op ol

Reputation: 767

Does C++ include C99 or C89?

I am about to learn C++.
There are two of the C language's syntax. Pointers and arrays.
They are defined on the C Standard with so many subclauses. I know that C++ is a superset of C(It may be a generalization). Then are these features same as C++? If so, what version of C++ has what version of C?

Upvotes: 1

Views: 2583

Answers (3)

John Bode
John Bode

Reputation: 123458

C and C++ are different enough that it's best to treat them as completely separate languages, rather than one being a super- or subset of the other. There are an infinite number of legal C programs that are not legal C++ programs, and an infinite number of legal C programs that are also legal C++ programs but behave differently or produce different results. While they do share a large common subset of syntax and semantics, there are enough differences between them that you should never be surprised if the same code gives you different results when compiled as C vs. C++.

A well-designed and well-written C++ program does not look or behave much like a well-designed and well-written C program.

Edit

Here's how I'd draw the relationships between the two:

+------------------------------+
|   Valid C++ Only             |
|                              | 
|      +-----------------------+-------+
|      | Valid C and C++ with  | Valid |
|      | identical behavior    | C     |
|      |                       | Only  |
|      |                       |       |
|      |                       |       |
|      +-----------------------+       |
|      | Valid C and C++ with  |       |
|      | different behavior    |       |
|      +-----------------------+-------+
|                              |
|                              |
+------------------------------+

The two languages have a very large common subset that behaves the same way between them. There's a smaller common subset that behaves differently. There's relatively little C that isn't also valid C++, but it's enough to cause problems if you're trying to port from C to C++ or support the same code between the two. Again, I think it's just better to treat them as completely separate languages. You're less likely to be surprised that way.

Upvotes: 3

eerorika
eerorika

Reputation: 238321

Does C++ include C99 or C89?

Currently, C++ includes neither in its entirety, although the common subset of C and C++ is significant. Some features are missing, and there are incompatible differences between the languages. The non-normative sections of the C++ standard "Compatibility / C++ and ISO C [diff.iso]" and "C standard library [diff.library]" lists (some of) the differences.

However, C++ is "based" on the C standard and does refer to it. Here is quote from latest standard draft:

[intro.scope]

C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:2018 Programming languages — C (hereinafter referred to as the C standard). C++ provides many facilities beyond those provided by C ...

[intro.refs]

The following documents are referred to in the text in such a way that some or all of their content constitutes requirements of this document. For dated references, only the edition cited applies. For undated references, the latest edition of the referenced document (including any amendments) applies.

  • ...
  • ISO/IEC 9899:2018, Programming languages — C
  • ...

The library described in ISO/IEC 9899:2018, Clause 7, is hereinafter called the C standard library.


what version of C++ has what version of C?

  • C++20 is based on C18
  • C++17 is based on C11
  • C++14 is based on C99
  • C++11 is based on C99
  • C++03 is based on C89/C90
  • C++98 is based on C89/C90

From historical perspective, (pre-standard) C++ existed before C was standardised and thus originally couldn't have been based on any standard. Both lanuages have evolved from that common root and have diverged into their own directions.

Also, note that even though many C idioms work in C++, they are not idiomatic in C++ and are considered a bad practice. As far as I understand, the reason for the high degree of compatibility (beyond ability to write common headers) is the ease of porting a C program to C++ allowing it to be converted with small incremental steps. For example, there is never a reason to write NULL in C++ and hardly ever a reason to write malloc.

Upvotes: 9

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16017

Stroustrup himself addresses this question on his homepage, https://www.stroustrup.com/bs_faq.html#C-is-subset. Key points (emphasis mine):

  • Mostly yes: "Except for a few examples such as the ones shown above (and listed in detail in the C++ standard and in Appendix B of The C++ Programming Language (3rd Edition)), C++ is a superset of C." Most of the incompatibilities are bad style even in C (implicit pointer conversions, missing function prototypes).
  • Regarding your question about which C version would be an ancestor of C++: "Please note that "C" in the paragraphs above refers to Classic C and C89. C++ is not a descendant of C99; C++ and C99 are siblings. C99 introduces several novel opportunities for C/C++ incompatibilities." Chiefly among the new incompatibilities are variable length arrays which are mostly unnecessary in C++ because C++ has vectors (but then, all C++ compilers I know support them because they implement them alreday for their C frontend, so why not).

Upvotes: 3

Related Questions