C is designed and developed by Dennis Ritchie in a company-Bell Labs in the year 1972 for the UNIX operating system. The recent and stable release of the C programming language is made in the year 2011, December.
C++ is designed and developed by Bjarne Stroustrup in the year 1985. Initially, C++ was called by the name “C with Classes” and later it is renamed to C++ in the year 1983. The symbol ++ indicates that an increment operator in C symbolically represents an advanced version of C. The main basis of C++ is, it allows both hardware access and abstraction and it carries effectively and that makes C++ stand and differentiate from other programming languages.
What is C?
C is a general-purpose programming language that is extremely popular, simple and flexible. It is machine-independent, a structured programming language which is used extensively in various applications.
C was the basic language to write everything from operating systems (Windows and many others) to complex programs like the Oracle database, Git, Python interpreter and more. It is said that ‘C’ is a god’s programming language. One can say, C is a base for the programming. If you know ‘C,’ you can easily grasp the knowledge of the other programming languages that uses the concept of ‘C’. It is essential to have a background in computer memory mechanisms because it is an important aspect when dealing with the C programming language.
What is C++ ?
C++ is a programming language developed by Bjarne Stroustrup in 1979 at Bell Labs. C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. It is a superset of C and that virtually any legal C program is a legal C++ program. C++ runs on a variety of platforms, such as Windows, Mac OS and the various versions of UNIX. It is a language that is case sensitive, free-form, containing OOPs, statically typed, compiled.
Features & Properties of C
- Bottom-up approach
- System programming language
- Does not support classes and objects
- Supports pointers
Features & Properties of C++
- Bottom-up approach
- Speed is faster
- Rich library support in the form of the standard template library
- Supports pointers & references
Key differences between C and C++ are:
- The older C-style way of including libraries looks like this:
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <ctype.h>
- C++ also used to use this format but moved to a new style shortly before the 1998 C++ standard. C++ libraries use just the library name, without the .h filename extension:
- #include <iostream>
- #include <iomanip>
- #include <vector>
And when including C libraries (in C++), now we drop the .h and add c to the front (to identify them as C libraries):
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cctype>
As C is a procedural language, it follows a top-down approach of programming. Here we take the problem and then break it into subproblems until we find single subproblems that can be solved directly. Then we combine the solutions to get the main solution. C++ follows a bottom-up approach to programming. In this, we start with low-level design or coding and then build on this low-level design to get a high-level solution.
The keywords help identify some of the C++-only features, things that were added into C++ but are not allowed in strict C coding. Such C++-only features (easily identified by the keyword lists) include:
o the Boolean type (bool)
o the 4 specific cast operations (compared to older C-style casting)
o classes and objects
o namespaces and using statements
o exception handling (with a try, catch, throw)
o using new and delete for dynamic memory management
C uses library functions malloc() and free() and other variations of those functions
Other C++ Features
In addition to the ones listed above, here are some other features already discussed in the course, which are C++ features (not legal in strict C coding):
- Function overloading: The ability to have multiple functions with the same name (in the same scope), by giving them different parameter lists
- Default values on parameters (making the parameters optional)
- An alternate form of variable initialisation
- int num(10); // equivalent to: int num = 10;
- Remember that this was a legal form in C++ and not legal in C
- Pass by reference — In C, there is no “by reference” – parameters and/or returns are either regular variables (pass-by-value) or pointers (pass-by-address). The array style parameter notation is still fine:
- void Func(int arr, int size); // legal in C
- void Func(int* arr, int size); // equivalent prototype
Data Security: As C does not support encapsulation so data behave as a free entity and can be manipulated by outside code. On another hand in the case of C++ encapsulation hides the data to ensure that data structures and operators are used as intended.
OOPs Feature Support: As C does not support the OOPs concept so it has no support for polymorphism, encapsulation and inheritance. C++ has support for polymorphism, encapsulation and inheritance as it is being an object-oriented programming language.
A namespace is a feature that groups the entities like classes, objects, and functions under some specific name. C does not contain the namespace feature, while C++ supports the namespace feature that avoids the name collisions.
Some important points to remember about using namespaces in C++ are:
- A namespace declaration can’t have access specifiers
- It is possible to nest namespace declarations within another namespace declaration
- Namespace declarations can be made only at the global scope
- The definition of a namespace can be fragmented into several units
- There is no need of adding a semicolon (;) after the closing brace of the definition of the namespace.
Memory allocation and De-Allocation: C supports calloc() and malloc() functions for the memory allocation, and free() function for the memory de-allocation. C++ supports a new operator for the memory allocation and delete operator for the memory de-allocation.
Type of Approach: C follows the top-down approach, while C++ follows the bottom-up approach. The top-down approach breaks the main modules into tasks; these tasks are broken into sub-tasks, and so on. The bottom-down approach develops the lower level modules first and then the next level modules.
Ease of Coding: We can say that C is a hands-on language and we can program it in whichever way we want. C++ consists of some high-level object-oriented programming constructs that help us to code high-level programs.
Thus if we say C is easy then C++ is also easier to code.
Program Division: A program in C is divided into functions and modules. These functions and modules are then called by the main function or other functions for execution. A C++ program is divided into classes and objects. The problem is designed into classes and the objects of these classes are the executing units that are created by the main functions and are executed.
Pointers And Reference Variables: Pointers are the variables that point to memory addresses. Both C and C++ support pointers and various operations performed on pointers. References act as aliases for the variables and point to the same memory location as a variable. C language only supports pointers and not references. C++ supports pointers as well as references.
Structures: Structures in C and C++ use the same concept. But the difference is, in C, as we cannot include functions as members. C++ allows structures to have functioned as its members.
Information Hiding: The features of abstraction and encapsulation can aid in information hiding by exposing only the required details and hiding the details like implementation, etc., from the user. This way we can enhance the security of data in our programs.
C++ puts great emphasis on data and uses abstraction and encapsulation for information hiding. C doesn’t put any emphasis on data and does not deal with information hiding.
Templates: It allows us to define classes and objects independent of the data type. Using templates, we can write a generic code and call it for any data type. C++ being object-oriented uses classes and objects and thus supports templates. C, on the other hand, doesn’t support the concept of templates.
Mapping: As far as the mapping of data with functions is concerned, C language is very complicated as it does not keep any focus on data. Whereas C++ has a good mapping of data and functions as it supports classes and objects that bind data and functions together.
Performance Comparison: Comparing C vs. C++ in terms of performance usually leads to the fact that C is faster than C++. However, in certain situations, C++ can win this race as well. Indeed, interpreted languages will never be faster than compiled ones. However, determining the speed difference of the two compiled languages is tricky. C vs. C++ speed highly depends on the produced code overall. A well-written C++ code can perform better or the same as a well-written C code.
Which should you learn?
You might now ask: should I learn C or C++? Learning C++ first might seem like the best option because it has more to offer than C. However, C helps you understand how hardware, especially CPU, works due to the straightforwardness of this language. You will get familiar with the low-level programming concept, learn about pointers, and proper memory allocation. Furthermore, C has fewer features that beginners will need to analyse.
However, the decision to learn C or C++ first depends on the way you want to work. There is no actual need to learn C before C++. Even if you start by learning C++, you will probably encounter some of the similar elements in syntax and semantics.
C vs C++: Head To Head Comparison
To read more on C++, click here.
By Akhil Sharma