C vs C++ Differences
In this page:
Procedural vs. Object-Oriented
C is a procedural language built entirely around functions operating on data step by step, while C++ was designed as an extension of C that layers object-oriented programming on top, letting you bundle data and the functions that operate on it together into classes and objects.
Example: Procedural vs. Object-Oriented
#include <stdio.h>
int square(int n) {
return n * n;
}
int main() {
printf("%d", square(5));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Standard I/O Differences
C relies on printf() and scanf() from stdio.h for all console input and output, using format specifiers like %d and %s to interpret the data, whereas C++ additionally offers the stream-based std::cout and std::cin from iostream, which use the << and >> operators instead of format strings.
Example: Standard I/O Differences
#include <stdio.h>
int main() {
printf("%d\n", 42);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variable Declaration Rules
Strict older C standards (like C89) required every variable in a function to be declared at the very top of its block before any executable statements, whereas C++ (and modern C standards like C99 and later) both relax this rule, letting you declare a variable right where you first need it.
Example: Variable Declaration Rules
#include <stdio.h>
int main() {
int x = 5;
printf("%d", x);
int y = 10;
printf("%d", y);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Overloading
C requires every function in a file to have a unique name, since it has no mechanism to tell two same-named functions apart, while C++ supports function overloading, letting you define several functions that share a name as long as their parameter lists differ in type or count.
Example: Function Overloading
#include <stdio.h>
int addInts(int a, int b) {
return a + b;
}
int main() {
printf("%d", addInts(2, 3));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Management
C manages dynamic memory through explicit library function calls — malloc() to allocate and free() to release — while C++ additionally provides the new and delete operators, which combine memory allocation with automatically calling a class's constructor or destructor at the same time.
Example: Memory Management
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = malloc(sizeof(int));
*ptr = 5;
printf("%d", *ptr);
free(ptr);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: