Difference between C and C++ with Examples | Programming Languages
Key difference: C and C++ are two different computer programming languages. C++ was developed from the C programming language; however they are quite different in nature. The most obvious difference is that C is a procedure oriented language, whereas C++ supports both procedural and object oriented programming, therefore it is often called a hybrid language.
include("ad4th.php"); ?>
C and C++ are two different computer programming languages. C was originally developed by Dennis Ritchie at AT&T Bell Labs between 1969 and 1973. C++ is another general-purpose programming language. It was developed from the original C programming language. It was developed by Bjarne Stroustrup at Bell Labs starting in 1979. C++ was originally named C with Classes, as it had been based on C. It was renamed C++ in 1983.
C is one of the oldest currently used programming languages and is one of the most widely used programming languages. It has been constantly used in applications that had previously coded in assembly language. This includes the UNIX computer operating system. C has also directly or indirectly influenced a lot of the later programming languages, such as C#, D, Go, Java, JavaScript, Limbo, LPC, Perl, PHP, Python, and Unix's C shell. Despite all these new languages, C still remains a popular programming language.
include("ad3rd.php"); ?>C++ originally started out as an enhancement to C. It was designed to be source-and-link compatible with C. However, it added classes, virtual functions, operator overloading, multiple inheritance, templates, exception handling, etc. Eventually, it developed enough to be considered a programming language in its own right. It was originally ratified in 1998 as ISO/IEC 14882:1998 certified programming language. Today, C++ is now commonly used for hardware design.
C is a general-purpose programming language that uses semicolon (;) as a statement terminator, as well as curly braces ({}) for grouping blocks of statements. It has facilities for structured programming and its design provides constructs that can map efficiently to typical machine instructions. It also allows lexical variable scope and recursion and has a static type system, which prevents many unintended operations.
However, as compared to C++, C has numerous limitations. As C is not object oriented, it does not support OOPS concepts. C does not support function and operator overloading. It cannot use functions inside structures. It does not support virtual functions and reference variables, or exception handling. It also does not support reference variables. In addition, C also does not encapsulation or data securing. In comparison, C++ supports all of these features.
Furthermore, C does not support NAMESPACE feature, whereas C++ does. A namespace is a definitive region which allows one to group identifiers (the names of types, functions, variables, etc). Namespaces can then be used to organize code into logical groups and to prevent name collisions. This is especially helpful when the code base includes multiple libraries, which ideally makes the code prone to name collisions.
The two languages also differ in the manner the address memory functions, input-output, and GUI programing. For instance, C uses calloc(), malloc() and free() functions for allocating and de-allocating memory, while C++ utilizes new and delete. C uses scanf() and printf() for input and output, while C++ uses cin>> and cout<< operators. C supports GTK tool for GUI programming, whereas C++ supports Qt tools for GUI programming. Another difference is that C requires one to declare all the variables at the top of the program, whereas in C++, the variables can be declared anywhere in the program.
Additionally, C++ is usually considered to be easier to learn, as it more user friendly than C. C++ also has numerous additional functions that makes coding easier, especially for someone who is new to the C framework. However, many claim that it does actually matter. The two languages differ so much, that they can in fact be two different languages independent of each other. One thing to keep in mind is that C is much more structured than C++, whereas C++ was designed to be more expressive and abstract than C.
Comparison between C and C++:
|
C |
C++ |
Founded |
Developed by Dennis Ritchie at AT&T Bell Labs between 1969 and 1973. |
Developed by Bjarne Stroustrup at Bell Labs starting in 1979. |
Source Code |
Free-format program source code |
Originally developed from the C programming language |
Language |
Procedure Oriented language |
Supports both procedural and object oriented programming paradigms; therefore it is often called a hybrid language. |
Approach |
Follows top-down approach. |
Follows bottom up approach. |
Relationship |
C is a subset of C++. It cannot run C++ code. |
C++ is a superset of C. C++ can run most of C code while C cannot run C++ code. |
Drive |
Function-driven language |
Object-driven language |
Focus |
Focuses on method or process rather than data. |
Focuses on data rather than method or procedure. |
Building Blocks |
Functions |
Objects |
Keywords |
Contains 32 Keywords |
Contains 52 Keywords |
OOPS Concepts |
As ‘C’ language is procedure oriented language, it does not support OOPS concepts such as class, object, Inheritance, Polymorphism, Data hiding, etc. |
As an object oriented language, C++ supports class, object, data hiding, polymorphism, Inheritance, abstraction, etc. |
Functions |
|
|
Memory functions |
Uses calloc(), malloc() and free() functions for allocating and de-allocating memory. |
Uses operators new and delete for the same purpose. |
Encapsulation |
Does not support. Data and functions are separate and free entities. |
Supports encapsulation. Data and functions are encapsulated together in form of an object. Objects class provides a blueprint of the structure of the object. |
Information hiding |
C does not support information hiding. Here, data are free entities and can be manipulated by outside code. |
Encapsulation hides the data to ensure that data structures and operators are used as intended. |
Data |
Supports built-in and primitive data types. Data is not secured due to non-object oriented |
Supports both built-in and user define data types. Data is secured (hidden) in C++ |
Level |
Low-level language |
Middle-level language |
Input-output |
‘C’ language uses scanf() and printf() for input and output. |
‘C++’ language uses cin>> and cout<< operators for input and output. |
Declaration of Variables |
C requires one to declare all the variables at the top of the program. |
In C++, the variables can be declared anywhere in the program before use. |
Multiple Declaration |
Multiple Declaration of global variables are allowed. |
Multiple Declaration of global variables are not allowed. |
Mapping |
Mapping between Data and Function is difficult and complicated. |
Mapping between Data and Function can be used using "Objects" |
GUI programming |
C supports GTK tool for GUI programming |
C++ supports Qt tools for GUI programming |
Inheritance |
Inheritance is not possible in C |
Inheritance is possible in C++ |
File Extension |
Has file extension .c |
Has file extension .cpp |
Default header file |
The default header file used in C language is stdio.h |
The default header file used in C++ is iosteam.h |
Some Examples of Difference between C and C++:
Examples |
C |
C++ |
Variable declaration |
Only at the top: int i; for(i=10; i<10; i++) |
Anywhere in the program:
for(int i=10; i<10; i++) |
Memory allocation |
Malloc: int *x = malloc( sizeof(int) ); int *x_array = malloc( sizeof(int) * 10 ); |
New: int *x = new int; int *x_array = new int[10]; |
Releasing Memory |
Free: free( x ); free( x_array ); |
Delete: delete x; delete[] x_array; |
Reference: Freefeast.info, cs-Fundamentals.com, studytipsandtricks.blogspot.in, cprogramming.com Image Courtesy: fossbytes.com, edushree.com
Comments
sam
Tue, 02/06/2018 - 10:57
Add new comment