DEV Community

Cover image for The Never Ending battle between C and C++
hebaShakeel
hebaShakeel

Posted on • Updated on

The Never Ending battle between C and C++

The other day, I and a friend of mine got caught up in an argument as to what extent C and C++ are different. So, I thought why not end this far-fetched argument for everyone and hence I sat to gather all the similarities and differences between the two and here I present them to YOU!

To start with, let me make it clear that both these languages are FANTASTIC in their own ways and learning them helps a new programmer understand complex computer science theories easily.
These languages are rarely used in the modern software industry, but they form the backbone of many popular programming languages and libraries.

Before diving deep into the differences between them, let's take a glimpse into the history of these languages.
C was originally designed for and implemented on UNIX operating system by Dennis Ritchie from 1969 to 1973.
While C++ was developed by Bjarne Stroustrup in 1979 after getting inspired by C and Simula. Interestingly, both the founders worked at the Bell Laboratories of AT&T.
C++ is inspired and derived from C ,so you can say that C is the FOUNDATION of C++.

1. Level of Language

C is a middle level language that binds the gap between machine level languages and high-level programming language. It has low level of data-abstraction (displaying only essential information and hiding the background details).
C++ is a high level language which has higher level of abstraction from machine language.

2. Programming Style

C supports procedural programming paradigm for code development as it was mainly designed for writing operating system kernels which means C follows a TOP-DOWN approach of compilation, where a typical program starts from the main() function and goes to the respective function down the order.

C++ supports both procedural and object-oriented programming paradigms.C++ being an advanced and OOP language supports features like Polymorphism (the ability of an object to take multiple forms), Encapsulation (binding of data and function together), and Inheritance (ability of a child object to automatically acquire all properties of its parent). Encapsulation in C++ makes the data more secure.

3. Memory Allocation and Deallocation

Dynamic memory allocation is done differently in both languages. In C, we use malloc() and calloc() functions for memory allocation and free() for deallocation.
While using C++, we use a different set of operators like New and Delete for memory allocation and deallocation respectively.

4. Exception Handling

C does not allow for direct exception handling but C++ does with its try and catch blocks. A few other things like, in C structures we can only have data members, but in C++ we can have both data members and functions.

5. Data Security

In C, data can be communicated between different blocks of code using global declarations.
Hence, data is less secure in C.
In C++, data is hidden. The data and function are encapsulated together in the form of an object. So, the data is not accessible to external functions and hence more secure than C.

6. Structures

C allows only data members in its structures. C structs cannot have functions. C structs cannot have static data members and Struct declaration does not establish a scope in C.
C++ structures can have both data members and functions. C++ structs can have static data members and Struct declaration establishes a scope in C++ .

Some Minor Differences!!

1. File Extension

File Extension in C is .c
File Extension in C++ is .cpp

2. Header File

Header File used in C is stdio.h
Header File used in C++ is iostream.h

3. Data Types

C supports only built-in data types.
C++ supports both built-in and user-defined data types.

4. Input/Output Standard Functions

scanf() and print() are used for input and output in C.
cin and cout are used for input and output in C++.


Comparing C and C++ is like comparing a floppy disk and a flash drive. Both languages have their own sets of precedence and pitfalls.
But why learn these languages when they are rarely used in the software industry?

The answer is simple.

Our favorite web browsers like Google Chrome, Mozilla Firefox, etc., kernels of our operating system, modern databases like MongoDB are written in C or C++. C is basically more suitable for writing programs where we need incredible stability and C++ is more suitable for writing programs for applications that work directly with the hardware.

Most importantly, these languages help a new programmer in understanding complex computer science theories easily.

Congratulations!!
We have come to an end of our discussion
Comment below your suggestions/views on this and let me know what you would like to hear from me Next!

Top comments (1)

Collapse
 
paddy3118 profile image
Paddy3118 • Edited

C++ has tried to be a superset of C, where there are a large set of programs that could be compiled by either compiler, and C level idiomd try to be carried over to C++.

In practice, some people write C++ as just enough C to get through the C++ compiler.