This commit is contained in:
Miloslav Ciz 2024-02-24 16:17:37 +01:00
parent ed1a6561bb
commit 940ac8279b
11 changed files with 1717 additions and 1681 deletions

View file

@ -54,7 +54,9 @@ C is **not** a subset of C++, i.e. not every C program is a C++ program (for sim
For portability sake it is good to try to write C code that will also compile as C++ (and behave the same). For this we should know some basic differences in behavior between C and C++.
One difference lies for example in [pointers](pointer.md) to string literals. While in C it is possible to have non-const pointers such as
One difference is e.g. in that type of character literals is int in C but char in C++, so `sizeof('x')` will likely yield different values.
Another difference lies for example in [pointers](pointer.md) to string literals. While in C it is possible to have non-const pointers such as
```
char *s = "abc";
@ -66,7 +68,18 @@ C++ requires any such pointer to be `const`, i.e.:
const char *s = "abc";
```
TODO: more examples
C++ generally has stronger typing, e.g. C allows assigning a pointer to void to any other pointer while C++ requires explicit type cast, typically seen with malloc:
```
int *array1 = malloc(N * sizeof(int)); // valid only in C
int *array2 = (int *) malloc(N * sizeof(int)); // valid in both C and C++
```
C allows skipping initialization (variable declarations) e.g. gotos or switches, C++ prohibits it.
And so on.
{ A quite detailed list is at https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B. ~drummyfish }
## Compiler Optimizations