C and C++ are closely related programming languages. C++ evolved from C and retains many concepts and syntax from C, while adding features such as classes, objects, inheritance, polymorphism, templates, and a larger standard library.
C is primarily a procedural programming language, while C++ supports procedural programming as well as object-oriented and generic programming.
C → Procedural Programming
C++ → Procedural + Object-Oriented + Generic Programming
C was developed before C++. C++ was developed as an extension of the C programming language.
The early version of C++ was known as C with Classes.
C mainly follows a procedural programming approach. Programs are organized around functions and procedures.
C++ supports multiple programming approaches, including:
C does not provide classes as a built-in language feature. C++ provides classes for creating user-defined types.
class Student {
public:
int age;
};
C++ allows programmers to create objects from classes.
class Student {
public:
int age;
};
int main() {
Student s1;
s1.age = 20;
return 0;
}
The object s1 is an instance of the Student class.
C++ provides built-in language features for object-oriented programming. Important concepts include:
C does not provide these concepts as built-in object-oriented language features.
C++ supports encapsulation through classes and access specifiers such as private, protected, and public.
class Account {
private:
double balance;
public:
void setBalance(double amount) {
balance = amount;
}
};
C++ supports inheritance, which allows one class to derive from another class.
class Animal {
};
class Dog : public Animal {
};
Inheritance is a built-in C++ class feature.
C++ supports polymorphism through features such as function overloading and virtual functions.
void display(int x) {
}
void display(double x) {
}
This is an example of function overloading in C++.
C++ allows multiple functions with the same name when their parameter lists are different.
void add(int a, int b) {
}
void add(double a, double b) {
}
Function overloading is supported by C++ but is not a feature of C.
C++ supports operator overloading for user-defined types. This allows operators to be given appropriate behavior for class objects.
For example, a class can define how the + operator works with its objects.
C++ supports templates for generic programming. Templates allow functions and classes to work with different data types.
template <typename T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}
Templates are an important feature of C++.
C commonly uses functions such as printf() and scanf() for standard input and output.
C++ commonly uses stream objects such as std::cout and std::cin.
// C++
#include <iostream>
std::cout << "Hello";
std::cin >> value;
Both C and C++ use header files, but their standard libraries provide different headers and facilities.
C example:
#include <stdio.h>
C++ example:
#include <iostream>
#include <stdio.h>
int main() {
int age;
printf("Enter age: ");
scanf("%d", &age);
printf("Age = %d", age);
return 0;
}
#include <iostream>
int main() {
int age;
std::cout << "Enter age: ";
std::cin >> age;
std::cout << "Age = " << age;
return 0;
}
Both languages provide low-level memory management capabilities. C commonly uses malloc(), calloc(), realloc(), and free().
C++ provides new and delete, along with modern smart pointers such as std::unique_ptr and std::shared_ptr.
Both C and C++ support structures, but their language rules differ. C++ structures can contain member functions and support features associated with classes.
struct Student {
int age;
void show() {
std::cout << age;
}
};
C++ provides references as a language feature. A reference acts as another name for an existing object or variable.
int number = 10;
int& ref = number;
ref = 20;
References are not part of the C language.
C++ provides built-in exception handling using try, catch, and throw.
try {
// Code
}
catch (...) {
// Handle exception
}
C does not provide this C++ exception-handling mechanism.
Both C and C++ provide standard libraries, but C++ has a much broader set of standard library facilities, including containers, algorithms, iterators, strings, and other utilities.
The C++ Standard Template Library is commonly known as STL.
C++ provides standard containers such as:
#include <vector>
std::vector<int> numbers = {10, 20, 30};
C commonly represents text using character arrays. C++ provides the std::string class as part of its standard library.
#include <string>
std::string name = "Rahul";
The C++ string class provides many convenient operations for handling text.
Many basic statements look similar in C and C++. For example, variables and control statements use familiar syntax.
int age = 20;
if (age >= 18) {
// Code
}
However, C++ adds many additional language features.
| Feature | C | C++ |
|---|---|---|
| Programming Style | Primarily procedural | Procedural, object-oriented, generic |
| Classes | No built-in classes | Supported |
| Objects | No built-in object model | Supported |
| Inheritance | Not a built-in class feature | Supported |
| Function Overloading | Not supported | Supported |
| Templates | Not supported | Supported |
| References | Not available | Supported |
| Exception Handling | No C++-style mechanism | Supported |
| STL | No | Yes |
Both C and C++ can produce highly efficient compiled programs. Performance depends on the implementation, compiler, algorithms, data structures, and how the program is written.
C++ provides additional abstractions while also allowing low-level programming techniques.
C is widely used in areas such as:
C++ is used in many areas, including:
Learning C helps students understand procedural programming, memory, pointers, arrays, functions, and low-level programming concepts.
Learning C++ builds on many of these concepts and adds object-oriented, generic, and modern programming features.
A learner who understands the fundamentals of C can recognize many familiar concepts when beginning C++.
C and C++ share many fundamental concepts, but C++ extends the programming model with classes, objects, inheritance, polymorphism, templates, references, exception handling, and a large standard library.
C
↓
Procedural Programming
↓
Functions + Structures + Pointers
C++
↓
Procedural Programming
+
Object-Oriented Programming
+
Generic Programming
+
Standard Library
Understanding the differences between C and C++ will make it easier to understand why C++ provides additional programming techniques while retaining many familiar C-style concepts.
Question: Which feature is directly supported by C++ but not by C?