C++ Programming/Programming Languages/Comparisons/C
C was essentially the core language of C++ when Bjarne Stroustrup decided to create a "better C". Many of the syntax conventions and rules still hold true, so we can even state that C was a subset of C++. Most recent C++ compilers can also compile C code, taking into consideration the small incompatibilities, since C99 and C++ 2003 are not compatible any more. You can also check more information about the C language on the C Programming Wikibook.
C++ as defined by the ANSI standard in 1998 (called C++98 at times) is very nearly, but not quite, a superset of the C language as it was defined by its first ANSI standard in 1989 (known as C89). There are a number of ways in which C++ is not a strict superset, in the sense that not all valid C89 programs are valid C++ programs, but the process of converting C code to valid C++ code is fairly trivial (avoiding reserved words, getting around the stricter C++ type checking with casts, declaring every called function, and so on).
In 1999, C was revised and many new features were added to it. As of 2004, most of these new "C99" features are not in C++. Some (including Stroustrup himself) have argued that the changes brought about in C99 have a philosophy distinct from what C++98 adds to C89, and hence these C99 changes are directed towards increasing incompatibility between C and C++.
The merging of the languages seems a dead issue, as coordinated actions by the C and C++ standards committees leading to a practical result did not happen and it can be said that the languages started to diverge.
Some of the differences are:
- C++ supports function overloading, this is absent in C, especially in C89 (it can be argued, depending on how loosely function overloading is defined, that it is possible to some degree to emulate these capabilities using the C99 standard).
- C++ supports inheritance and polymorphism.
- C++ adds keyword class, but keeps struct from C, with compatible semantics.
- C++ supports access control for class members.
- C++ supports generic programming through the use of templates.
- C++ extends the C89 standard library with its own standard library.
- C++ and C99 offer different complex number facilities.
- C++ has bool and wchar_t as primitive types, while in C they are typedefs.
- C++ comparison operators returns bool, while C returns int.
- C++ supports overloading of operators.
- C++ character constants have type char, while C character constants have type int.
- C++ has specific cast operators (static_cast, dynamic_cast, const_cast and reinterpret_cast).
- C++ adds mutable keyword to address the imperfect match between physical and logical constness.
- C++ extends the type system with references.
- C++ supports member functions, constructors and destructors for user-defined types to establish invariants and to manage resources.
- C++ supports runtime type identification (RTTI), via typeid and dynamic_cast.
- C++ includes exception handling.
- C++ has std::vector as part of its standard library instead of variable-length arrays as in C.
- C++ treats sizeof operator as compile time operation, while C allows it be a runtime operation.
- C++ has new and delete operators, while C uses malloc and free library functions.
- C++ supports object-oriented programming without extensions.
- C++ does not require use of macros, unlike C, that uses them for careful information-hiding and abstraction (especially important for C code portability).
- C++ supports per-line comments denoted by //. (C99 started official support for this comment system, and most compilers support this as an extension.)
- C++ register keyword is semantically different to C's implementation.
Choosing C or C++
It is not uncommon to find someone defending C over C++ (or vice versa) or complaining about some features of these languages. There is no scientific evidence to put a language above another in general terms; the only reason that does have some traction is the possibility of deep changes or unknown bugs in a language that is still very recent. In the case of C or C++ this is not the case, as both languages are very mature. Though both are still evolving, the new features keep a high level of compatibility with old code, making the use of those new constructs a programmer's decision. It is not uncommon to establish rules in a project to limit the use of parts of a language (such as RTTI, exceptions, or virtual-functions in inner loops), depending on the proficiency of the programmers or the needs of the project. It is also common for new hardware to support lower level languages first. Due to C being less extensive and lower level than C++, it is easier to check and comply with strict industry guidelines and automate those steps. Another benefit of C is that it is easier for the programmer to do low level optimizations, though most C++ compilers can guarantee near perfect optimizations automatically, a human can still do more and C has less complex structures.
Any of the valid reasons to choose a language over another is mostly due to programmer's choice that indirectly deals with choosing the best tool for the job and having the resources needed to complete it. It would be hard to validate selecting C++ for a project if the available programmers only knew C. Even though in the reverse case it might be expected for a C++ programmer to produce functional C code, the mindset and experience needed are not the same. The same rationale is valid for C programmers and ASM. This is due to the close relations that exist in the language's structure and historic evolution.
One could argue that using the C subset of C++, in a C++ compiler, is the same as using C, but in reality we find that it will generate slightly different results depending on the compiler used.