C vs C++ – 15 Key Differences
In this lesson: Difference between C and C++ — procedural vs OOP, printf vs cout, malloc vs new, struct vs class — table and code.
C and C++ in one paragraph
C was created by Dennis Ritchie at Bell Labs in 1972 to write the UNIX operating system. It is small, fast and close to the hardware. C++ was created by Bjarne Stroustrup, starting in 1979, as "C with Classes": it keeps nearly all of C and adds object-oriented programming and many safety and productivity features. C++ is standardised by ISO; major versions are C++98, C++11, C++14, C++17, C++20 and C++23.
Difference between C and C++ (table)
| # | Point | C | C++ |
|---|---|---|---|
| 1 | Paradigm | Procedural | Multi-paradigm: procedural + object-oriented + generic |
| 2 | Classes and objects | No (only struct with data) | Yes — classes with data and functions |
| 3 | Encapsulation / data hiding | No access control | private, protected, public |
| 4 | Inheritance and polymorphism | Not supported | Supported (including virtual functions) |
| 5 | Input / output | printf(), scanf() in <stdio.h> | cout, cin in <iostream> (C functions still work) |
| 6 | Dynamic memory | malloc(), calloc(), free() | new, delete; smart pointers |
| 7 | Function overloading | Not allowed | Allowed |
| 8 | Default arguments | No | Yes |
| 9 | References | No (pointers only) | Yes: int& r = x; |
| 10 | Namespaces | No | Yes: std::, your own |
| 11 | Exception handling | No (error codes) | try, catch, throw |
| 12 | Templates / generic code | No (macros only) | Function and class templates |
| 13 | Standard library | C library (string.h, math.h …) | C library + STL: vector, map, sort… |
| 14 | String type | char arrays | std::string class |
| 15 | File extension | .c | .cpp, .cc, .cxx |
Side by side: input and output
/* C */
#include <stdio.h>
int main(void) {
int age;
printf("Enter age: ");
scanf("%d", &age);
printf("Next year you will be %d\n", age + 1);
return 0;
}// C++
#include <iostream>
int main() {
int age;
std::cout << "Enter age: ";
std::cin >> age;
std::cout << "Next year you will be " << age + 1 << "\n";
return 0;
}C++ streams know the type of each variable, so there are no format specifiers like %d to get wrong, and no & needed for input.
Side by side: dynamic memory
/* C */ int *marks = (int *) malloc(5 * sizeof(int)); /* ... */ free(marks);
// C++ (classic) int* marks = new int[5]; // ... delete[] marks; // C++ (modern — no manual free at all) std::vector<int> marks(5);
new calls constructors and needs no cast or sizeof. Modern C++ goes further: containers and smart pointers free memory automatically.
struct in C vs class in C++
/* C: data only; functions live outside */
struct Student { char name[30]; int marks; };
void print(struct Student s) { printf("%s %d\n", s.name, s.marks); }// C++: data and behaviour together, with access control
class Student {
std::string name;
int marks;
public:
Student(std::string n, int m) : name(n), marks(m) {}
void print() const { std::cout << name << " " << marks << "\n"; }
};In C++, a struct can also have member functions; the only difference from class is that members are public by default.
Overloading and references
// Only possible in C++
int area(int side) { return side * side; }
int area(int l, int b) { return l * b; }
void addBonus(int& marks) { marks += 5; } // reference: no * or &x needed
int m = 70;
addBonus(m); // m becomes 75In C you would need two function names (area_square, area_rect) and a pointer parameter (void addBonus(int *m) called as addBonus(&m)).
What is the same in both
- Basic data types (
int,char,float,double), operators and expressions. if,switch,for,while,do-while,break,continue.- Arrays, pointers and pointer arithmetic.
- Functions, recursion and the preprocessor (
#include,#define). - The C standard library, available in C++ as
<cstdio>,<cmath>,<cstring>and so on.
Which one to use?
| Use C when… | Use C++ when… |
|---|---|
| Writing very small embedded/firmware code or an OS kernel | Building larger applications, games, desktop software |
| The platform only has a C compiler | You want classes, STL containers and algorithms |
| You are studying how memory and hardware work at the lowest level | Your syllabus (CBSE Class 11–12 CS, many B.Tech courses) uses C++ |
Common misconceptions
- "C++ is just C with classes" — modern C++ also has templates, the STL, lambdas, smart pointers and much more.
- "C code always compiles in C++" — not if it uses C++ keywords as names or relies on implicit
void*conversion. - "
coutis slower, so C++ is slower" — I/O speed depends on settings likestd::ios::sync_with_stdio(false); computation speed is the same. - "
structin C++ cannot have functions" — it can.
Write a program that stores 5 students' names and marks. First write it in C using struct, char[] and printf. Then rewrite it in C++ using a class, std::string, std::vector and cout. Count how many lines each version needs and note which one was easier to get right.
Summary
- C is procedural; C++ adds OOP, generic programming and many safety features.
- C++ has classes, references, overloading, namespaces, exceptions, templates and the STL.
printf/scanf→cout/cin;malloc/free→new/deleteor, better, containers and smart pointers.- Most C code is valid C++, but not all.
- For the same algorithm, both are equally fast.
Related: introduction to C++, OOP concepts, C tutorial: C vs C++.
Frequently Asked Questions
What is the main difference between C and C++?
Can a C program run as a C++ program?
void* returned by malloc to an int* needs a cast in C++, and names like new, class or delete are keywords in C++, so C code using them as variable names will fail.Which is faster, C or C++?
Should I learn C before C++?
std::string, std::vector and classes) is also fine, and many schools teach C++ first in Class 11–12.