C++ vs C
Key differences between C and C++
Interview Relevant: Common comparison question
C++ vs C: Key Differences
While C++ is built on C, there are significant differences.
Major Differences
- OOP Support: C++ has classes, inheritance, polymorphism; C is procedural
- Function Overloading: C++ supports it; C does not
- References: C++ has reference types; C only has pointers
- Memory Management: C++ has new/delete; C uses malloc/free
- Standard Library: C++ has STL with containers; C has limited stdlib
- Namespaces: C++ supports namespaces; C does not
- Exception Handling: C++ has try/catch; C uses error codes
- Templates: C++ has templates for generic programming
Code Examples
Comparison of memory allocation styles.
cpp
1// C style
2int* arr = (int*)malloc(10 * sizeof(int));
3free(arr);
4
5// C++ style
6int* arr = new int[10];
7delete[] arr;
8
9// Modern C++ style
10std::vector<int> arr(10); // No manual memory management