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 about that? Is it possible to write a valid C program that is not valid C++ code?
EDIT: This is really similar to, but not an exact duplicate of this question.
C is one of the earliest and most widely used programming languages. C is the fourth most popular programming language in the world as of January 2022. Modern languages such as Go, Swift, Scala, and Python are not as popular as C.
The C programming language will turn fifty years old in 2022. Yet despite its long history, C remains one of the top "most-used" programming languages in many "popular programming languages" surveys. For example, check out the TIOBE Index, which tracks the popularity of different programming languages.
C is a general purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. It was named 'C' because many of its features were derived from an earlier language called 'B'.
C++ is a object oriented programming and supports OOPS concepts like polymorphism, encapsulation, and inheritance. C is a subset of C++. C++ is superset of C. All code of C can run in C++ but vice versa may or may not be true.
In general, yes C code is considered C++ code.
But C is not a proper subset in a strict sense. There are a couple of exceptions.
Here are some valid things in C that are not valid in C++:
int *new;//<-- new is not a keyword in C
char *p = malloc(1024); //void * to char* without cast
There are more examples too, but you get the idea.
I previously wrote a more extensive answer in a similar question here.
Also note that C99 adds several features which aren't permitted in C++ (or are only supported using vendor extensions), such as builtin _Complex
and _Imaginary
data types, variable-length arrays (arrays sized at runtime rather than compile time), flexible array members (arrays declared as the last member of a struct that may contain an unspecified number of elements), and more.
For the exhaustive list of incompatibilities between C and C++, including changes with C99, see http://david.tribble.com/text/cdiffs.htm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With