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 standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", square(5));
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d\n", 42);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%d", x);
// Declare y and set it to 10
int y = 10;
// Print formatted text to the screen
printf("%d", y);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
int addInts(int a, int b) {
// Send a + b back to the caller
return a + b;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", addInts(2, 3));
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = 5;
// Print formatted text to the screen
printf("%d", *ptr);
// Release the heap memory so it can be reused
free(ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Assuming C code always compiles as C++, when C++ is stricter about things such as
void*conversion. - Using
printfin C++ wherecoutis safer for types. - Forgetting that C++ does not allow implicit conversion from
void*returned bymallocwithout a cast.
- Additional C topics include
goto, header files, multi-file programs, and static and inline functions. - Signal handling, date and time functions, and random numbers add system-level features.
- Comparing C with C++ shows how the two languages differ.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: