Introduction
C evolved via Dennis Ritchie in 1972 for making utilities able to go for walks on Unix. C++ was evolved by way of Bjarne Stroustrup in 1979. They wanted an extension of C that was both flexible and efficient.
Differences between C and C++
Definition
C is an excessive-stage programming language. It became at the start used for writing Unix programs but is now used to write down programs for almost every available platform. compared to most preceding languages, C is simpler to read, greater bendy (can be used for a wide sort of functions), and more efficient at using memory.
C++, pronounced "C Plus Plus," is a programming language that became built off the interval. The syntax of C++ is almost the same as C, but it has object-oriented functions, which allow the programmer to create objects in the code. This makes programming easier, greater efficient, and, some would even say, extra fun. because of the power and flexibility of the language, maximum software program programs nowadays are written in C++.
Also read - File Handling in CPP
Exception Handling
C uses functions for error handling. C++ has well-designed strive-seize blocks that make debugging lots easier.
File Extensions
All C packages are stored with a .c extension. C++ uses the .cpp extension.
Variables
In C, we need to claim all variables at the start of the function block. In C++, the variables may be declared everywhere so long as they are announced before being used inside the code.
Data Types
With C, you could outline your use of struct, union, or enum.
// Structures
struct stud_id
{
char name[10];
int class;
int Entry_number;
char address[20];
};
C++ helps person-described facts kinds as nicely. C++ consumer-defined facts types include:
// Classes
class <classname>
{
Private:
Data_members;
Member_functions;
Public:
Data_members;
Member_functions;
};
// Structures
struct stud_id
{
char name[10];
int class;
int Entry_number;
char address[20];
};
// Unions
union employee
{
int id;
double salary;
char name[20];
}
// Enumerations
enum week_days{sun, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
enum week_days A;
A = monday;
cout << A;
return 0;
}
//Typedef
typedef <type> <newname>;
typedef float balance;
Strings
C represents string literals the usage of char[]. In C++, strings are objects of the class string, defined inside the header document. That is how strings are represented in C:
char str[] = "ninjas";
char str[50] = "ninjas";
char str[] = {'n','i','n','j','a','s','\0'};
char str[14] = {'n','i','n','j','a','s','\0'};
In C++, strings are represented as follows:
string s1;
string s2("Hello");
string s3 = "Hello";
string s4(s2);
string s5 = s2;
Function Overloading
Function overloading is a shape of polymorphism that permits a function with the identical call to be defined for various parts. Overloaded functions have the same name, however, unique parameters. C does not help characteristic overloading. However, C++ does.
In the example underneath, we have identical function names, however, different data types.
#include <iostream>
using namespace std;
class Addition
{
public:
int sum(int a,int b)
{
return a+b;
}
int sum(int a, int b, int c)
{
return a+b+c;
} };
int main(void)
{
Addition obj;
cout<<obj.sum(a, b)<<endl;
cout<<obj.sum(a, b, c);
return 0;
}
Operator Overloading
Operator overloading lets you change the way an operator works for user-defined functions. Though C does not help this, C++ does.
The main( )function
Operator overloading permits you to exchange how an operator works for user-described functions. Though C does now not help this, C++ does.
Data Security and Encapsulation
Encapsulation aids in hiding customer data and is s essential function of OOP. C does now not guide encapsulation. C++ uses lessons that package deal facts and the parts working in this record into a single unit.
Input and Output Operations
C makes use of printf and scanf for input and output, respectively. C++ makes use of cin and cout.
Memory Management
As stated above, each C and C++ require manual memory control. The shallow difference is how they do it. C uses malloc() and malloc() functions for dynamic memory allocation. C++ uses the new operator and free() for memory allocation and the delete operator for memory de-allocation.
Also Read - C++ Interview Questions








