What is the difference between C and C++?
| Point | C | C++ |
|---|---|---|
| Style | Procedural | Procedural, object oriented, generic |
| Classes | No | Yes |
| Overloading | No | Yes |
| References | No | Yes |
| Templates, exceptions | No | Yes |
| Strings and containers | char arrays, own code | std::string, STL |
| Memory | malloc and free | new, delete, smart pointers |
| I/O | printf, scanf | cout, cin, print |
This C code is not valid C++, because C++ does not convert void* automatically:
#include <stdlib.h>
int main() {
int* p = malloc(sizeof(int));
free(p);
}
Output
Bad.cpp: In function 'int main()':
Bad.cpp:4:20: error: invalid conversion from 'void*' to 'int*' [-fpermissive]
4 | int* p = malloc(sizeof(int));
| ~~~~~~^~~~~~~~~~~~~
| |
| void*
See C++ vs C.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |