Jump to content

C++ Programming/Programming Languages/Comparisons/C

From Wikibooks, open books for an open world

C 89/99

[edit | edit source]

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.

Note:
In practice, much C99 code will still compile with a C++ compiler, but the language is no longer a proper subset. Compatibility is not guaranteed.

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++
[edit | edit source]

It is fairly common to find someone recommending the use of C instead of C++ (or vice versa), or complaining about some features of these languages. There is no decisive reason to prefer one language over the other in general. Most scientific studies that attempt to measure programmer productivity as a function of programming language rank C and C++ as essentially equal. C may be a better choice for some situations, for example kernel programming, like hardware drivers, or a relational database, which do not lend themselves well to object oriented programming. Another consideration is that C compilers are more ubiquitous so C programs can run on more platforms. Although both languages are still evolving, any new features added still maintain 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 simpler and lower level than C++, it is easier to check and comply with industry guidelines. Another benefit of C is that it is easier for the programmer to do low level optimizations, though most C++ compilers can guarantee nearly perfect optimizations automatically.

Ultimately it is the programmer's choice to decide what tool is the best for the job. It would be hard to justify selecting C++ for a project if the available programmers only know 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 historical evolution.

One might think that using only the C subset of C++ compiled with a C++ compiler is the same as just using C, but in reality it can generate slightly different results depending on the compiler used.