While C++ is largely a superset of C, there are a few things you can do in C that are either not allowed or discouraged in C++ due to stricter type safety, object-oriented features, or other design choices. Here are some key differences:
1. Implicit Conversion from void* to Other Pointer Types
2. Variable-Length Arrays (VLAs)
3. Using const Variables as Array Sizes
4. Different Handling of struct and typedef
5. No Function Overloading in C
6. No Namespaces in C
- C does not have namespaces, while C++ does (to avoid naming conflicts).
7. Different main() Function Rules
8. No References in C
- C++ has references (
int&), but C only has pointers.
9. No Exception Handling in C
- C++ supports
try, catch, and throw, while C uses error codes or setjmp/longjmp.
10. Different Rules for enum
11. No Member Functions or this Pointer in C
- C does not support member functions or the implicit
this pointer (since it lacks classes).
12. Different Rules for String Literals
- In C, string literals are of type
char[] (modifiable in some compilers, though technically undefined behavior).
- In C++, they are
const char[], and modifying them is undefined behavior.
13. No bool Type in C (Pre-C99)
- Older C standards (before C99) did not have a built-in
bool type (C++ always had bool).
14. No Default Arguments in C
- C++ allows default function arguments, but C does not.
15. No Templates in C
- C++ supports templates (generic programming), while C does not.
Conclusion
While C++ is mostly backward-compatible with C, it enforces stricter type safety and introduces features that prevent some C-style practices. Some C code will not compile in C++ without modification, particularly around pointer casts, struct usage, and implicit conversions.
Would you like a deeper explanation of any of these?