C vs C++

C
C++

This is an objective comparison of the applications, usage and language characteristics of C and C++. The origins and development trajectory of the two programming languages are also discussed.

Comparison chart

Edit this comparison chart

C

User Rating (257):

C++

User Rating (238):
Designed by Dennis Ritchie Bjarne Stroustrup
Paradigms Imperative (procedural) systems implementation language Multi-paradigm, object-oriented programming, generic programming, procedural programming, functional programming, metaprogramming
Typing Discipline Static, Weak static, strong, unsafe, nominative
Influenced awk, csh, C++, C#, Objective-C, BitC, D, Concurrent C, Java, JavaScript, Limbo, Perl, PHP Ada 95, C#, Java, PHP, D, Aikido
Major Implementations GCC, MSVC, Borland C, Watcom C GNU Compiler Collection, Microsoft Visual C++, Borland C++ Builder, Intel C++ Compiler, LLVM/Clang
Influenced by B (BCPL,CPL), ALGOL 68, Assembly C, Simula, Ada 83, ALGOL 68, CLU, ML
Appeared in 1972 1985
Garbage Collection Manual; allows better management of memory. No
Speed C applications are faster to compile and execute than C++ applications +-5% when compare with C if you know how to make a good use of C++.The performance of C++ and C are equal, since compilers are mature.
Usual filename extensions .c .cc, .cpp, .cxx, .h, .hh, .hpp
Programming-include use #include<stdio.h> include

Contents: C vs C++

edit Origins of C and C++

Dennis Ritchie of the Bell Labs designed the C, a general purpose computer programming language in 1972 for use with UNIX, an operating system of then. C is predominantly used for system software programming, but is also very useful for creating general application software. Some of the adjectives used to describe C are block structured, imperative & procedural language.

C++(originally named "C with Classes" and still known as the superstructure of C in computer circles) was developed as an enhancement of C by Bjarne Stroustrup in 1983 at the Bell Labs. Stroustrup, in 1979, started by adding classes, virtual functions, operator overloading, multiple inheritance, templates, exception handling etc. The C++ programming language standard was ratified as ISO/IEC 14882:1998 in 1998 and the current version is the 2003 version, ISO/IEC 14882:2003 which is infact the corrected version of the C++ 1998. The "Library Technical Report 1", released in 2005 gives details of extensions to the standard library without being a part of the standar version. A new version of the standard (informally known as C++0x) is under development. C++ has been a highly successful commercial programming language since 1990. Though C++ is royalty-free, its documentation is not freely available.

edit Usage of C vs. C++

C proved very useful in running applications coded in assembly language because of its strengths like a simple compiler, lower access levels of memory, lower run time support and an efficient constructing language that was in sync with the hardware instructions. Another of its credits is that it is a highly portable (compatible with a variety of OS & Platforms) with very minimal source code changes required. Thus it has enabled remote operations & independence from the hardware. C is also compliant to a variety of standards, making it work with everything.

C++ is known as a mid-level language. Due to the fact that the C++ comprises of both high-level and low-level language features. Some of the adjectives used to describe C++ are static typed, free-form, multi-paradigm and supporting procedural programming.

Stroustrup, while programming for his Ph.D thesis, found that the Simula language had high level features helpful for large software development, but was too slow for practical use, while the BCPL (language) was fast, but too low-level and thus unsuitable for large software development. In Bell labs, he had to analyze the UNIX kernel with respect to distributed computing which created further problems and he set out to enhance C (due to its ultra portable nature) with features from the Simula. C++ was created in 1983 with additional features like virtual functions, function name and operator overloading, references, constants, user-controlled free-store memory, improved type checking and single-line comments with two forward slashes (//). The Cfront (commercial version) was released in 1985 with the class, derived class, strong type checking, inlining, and default argument features. 1985 also saw the release of the The C++ Programming Language, an important reference to the language in the absence of an official standard. This was followed by the release of the C++ 2.0 in 1989 with features like multiple inheritance, abstract classes, static member functions, const member functions and protected members. Features like templates, exceptions, namespaces, new casts and Boolean type were added post 1990.

Along with the language, its library also evolved, with several additions like the stream I/O library, the Standard Template Library etc.

The first editions of the book K & R written by Dennis Ritchie & Brian Kernighan (original name: The C Programming Language) describes their version of C as the K & R C with full specifications, while the later editions include the ANSI (American National Standards Institute) C standards. Some of the salient features described are the introduction of various data types, removal of several semantic ambiguities, omission of other function declarations etc. Even after the introduction of the ANSI C, the K & R C continued to be the most portable programming language for programmers due to its wider compatibilities.

K&R function declarations did not include any information about function arguments leading to non-performance of function parameter type checks, although some compilers issued a warning message if a local function was called with the wrong number of arguments or if multiple calls to an external function used different numbers of arguments. Tools such as UNIX's lint utility were created for checking the consistency of functions used across multiple source files.

edit Language Characteristics

edit Characteristics of C

Some of the important characteristics of C are as follows:

  1. Structured programming facilities
  2. Confirming to the ALGOL traditions
  3. Short circuit evaluation – usage of only one operand if the result can be determined with it alone
  4. Static typing system for avoiding unintended operations
  5. Value passed parameters with relevance to pointer value passing
  6. Heterogeneous data combination & manipulation
  7. Reserved keywords and free-format source text
  8. Larger number of compound operators, such as +=, ++
  9. Huge variable hiding capacity, though function definitions being non-nestable
  10. Character – integer usage similar to assembly language
  11. Low-level access to computer memory via machine addresses and typed pointers
  12. Function pointers allow rudimentary forms of closures & polymorphic runtime
  13. Pointer arithmetic defined Array indexing (secondary notion)
  14. Standardized processor for defining macros, including source code files & conditional compilations
  15. Complex Input/Output and mathematical functions with consistent delegation to library routines
  16. Syntax same as “B” (C’s predecessor) but different from ALGOL e.g.: { ... } replaced begin ... end, && and || replaced and & or, which
  17. While B used & and | in both meanings, C made them syntactically distinct from the bit-wise operators
  18. Similarities to Fortran e.g: the equal- sign for assignment (copying) & two consecutive equal-signs to test for equality (compare to EQ) or the equal-sign in BASIC)

Other unofficial features added with time were:

  1. void functions
  2. Functions returning struct or union types instead of pointers
  3. Assignments enabled for struct data types
  4. const qualifier to make an object read-only
  5. Enumerated types
  6. Creationg of tool to avoid the inherent problems of the language

Soon C became powerful enough to have the UNIX Kernel (written in a assembly language) re-written making it one of the first OS Kernels written in a language apart from the assembly languages.

edit Characteristics of C++

  1. C++ is designed to be a statically typed, general-purpose language that is as efficient and portable as C
  2. C++ is designed to directly and comprehensively support multiple programming styles (procedural programming, data abstraction, object-oriented programming, and generic programming)
  3. C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly
  4. C++ is designed to be as compatible with C as possible, therefore providing a smooth transition from C
  5. C++ avoids features that are platform specific or not general purpose
  6. C++ does not incur overhead for features that are not used
  7. C++ is designed to function without a sophisticated programming environment

Polymorphism, one of the prominent qualities of C++, enablesmany implementations with a single interphase and for objects to act according to circumstances. C++ supports both static (compile-time) and dynamic (run-time) polymorphisms. Compile-time polymorphism does not allow for certain run-time decisions, while run-time polymorphism typically incurs a performance penalty. C++, though considered a superset of C, there exist a few differences causing some valid C codes to be invalid in C++ or to behave differently in C++. Issues like the C++ defining new keywords namely new & class, that are used as identifiers in C. C and C++ codes can be intermixed by declaring any C code that is to be called from/used in C++ with C linkage & by placing it within an extern "C" { /* C code */ } block.

edit Developments in the two languages

With time, standardization became more and more important because of large numbers of extensions and a random library with growing popularity of the language and the lack of precise implementation of compilers as per the specifications. One of the aims of the C standardization process was to produce a superset of K&R C, incorporating many of the unofficial features introduced subsequently. However, the standards committee included several new features like function prototypes, void pointers, support for international character sets and locales and a more capable preprocessor. The syntax for parameter declarations was also augmented. Post 1970s, C replaced BASIC as the leading language for microprocessor programming and became popular with its collaboration with the IBM PCs. Meanwhile, Bjarne Stroustrup and others at Bell Labs began work on creating the C++, which added object-oriented programming language constructs to C. Further, ANSI formed a committee in 1983 called X3J11, to establish a standard specification of C and in 1989, the standard was ratified as ANSI X3.159-1989 "Programming Language C." This is the version of C that is often referred to as ANSI C, Standard C or C89. C90, introduced in 1990, was ame as C89 barring a few minor changes. While C++ evolved rapidly, C remained static until 1995 when the Normative Amendment 1 created a new standard which underwent further revision, leading to the publication of ISO 9899:1999 in 1999. This standard is commonly referred to as "C99." It was adopted as an ANSI standard in March 2000. Some of the newer functions are mentioned below:

C++, while continuing to evolve to meet the requirements of the future, a newer version called C++0x denoting that it is expected to be released before 2010 is currently being developed. Indications suggest that C++ will continue to capitalize on its multi-paradigm nature and notable improvements may bee native support for threading and concepts thereby making working with templetes easier. More controversially, adding garbage collection is currently under heavy discussion. A group called Boost.org, that advises the C++ standards committee on good features and improvements required, is working extensively to develop C++ in its current form with expanded functional and metaprogramming abilities.

In The Design and Evolution of C++ (1994), Bjarne Stroustrup describes some rules that he used for designing the C++. Knowing the rules helps to understand why C++ is the way it is. Much more detail can be found in The Design and Evolution of C++.

edit Influences

C’s influence can be traced to works like awk, csh, C++, C#, D, Objective-C, Concurrent-C, BitC, Java, JavaScript, Limbo, Perl, PHP etc. Some of the major implementations of C include the Borland C, Watcom C, GCC & MSVC. C++, to its credit has influenced other works like D, C#, ADA 95, Aikido, Java & PHP.

edit Criticisms of C vs. C++

Despite its popularity, C has been criticized for having desirable operations being too hard to achieve and undesirable operations being too easy to accidentally invoke thereby involving more programmer skill, experience, effort, and attention to detail than other programming languages for the safe & effective use of the language.

When object-oriented languages became popular, C++ was an extension of C that provided object-oriented capabilities with C++ originally implemented as a preprocessor -- source code was translated into C, and then compiled with a C compiler.

C++ being derived from C, also happens to inherit most of the criticisms leveled against C. But since the language is actually a composition of two different languages, along with the load of huge programs, often end up making the compilation huge and inappropriate in terms of pure size. When this problem is tried to be avoided, by disabling some of the fringe codes, it was again criticized for losing out on several important utilities. The creator of C++ also feels that C++ is justified to be a complex language since the modern day programming requirements have also increased in a huge manner when compared to the yesteryears.

edit References

Comments: C vs C++

Your IP address will be logged and parts of it shown publicly.
Write <youtube v=YouTubeVideoID /> to embed a YouTube video in your comment.
Comments with links (URLs) are not allowed because we get overrun by spam.

February 19, 2014, 5:39am

Gr8!

— 202.✗.✗.244
0

September 18, 2012, 10:26am

>if you are targeting performances then you can use C++ as long as you write C code compiled with a C++ compiler

Or use template to reduce redundancy codes, compile time polymorphism would not
cause runtime impact if you know what are you doing about.C++ is not slower
than C or faster than C, they are even, if you are good enough to handle it.

— 14.✗.✗.89
0

July 26, 2012, 2:30pm

using good material

— 115.✗.✗.125
0

August 25, 2009, 11:01am

A real-life project written in C and C++ may help to get the point.

TrustLeap G-WAN is a Web Server which is faster (in user-mode) than IIS 7.0 (in the kernel).
G-WAN ANSI C89 ('edit & play') scripts are 5x faster than ASP.Net C#.

G-WAN is up to 38x faster than Apache.
G-WAN is up to 25x faster than Nginx.

G-WAN was started in C++ and then converted to C because of the '++' overhead. The gain was not only in footprint (currently 106 KB), but also in performances (~180%).

If you want to know what makes C++ so bad, just try virtual inheritance and virtual functions, and then compare the machine code generated by the C++ compiler with a clean C implementation of the same features (the C++ code is twice larger -and slower).

Conclusion: if you are targeting performances then you can use C++ as long as you write C code compiled with a C++ compiler.

Linus is a programmer that make systems work, and Bjarne is a programmer that uses the systems created by programmers like

— 85.✗.✗.100
0
Stay informed Related Comparisons
Follow Diffen
Make Diffen Smarter.

Log in to edit comparisons or create new comparisons in your area of expertise!

Sign up »
Top 5 Comparisons Recently Compared

share

Up next

PHP vs. Ruby