Notes on C / C + + Programming: write C programs that can’t be compiled in C + +, I’ll teach you!

Time:2021-4-8

Although C + + is designed to be backward compatible with C, many C programs may produce compiler errors when compiled with C + + compiler. Here are some of them.

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

1)In C + +, calling functions is a compiler error before declaring functions. But in C, it compiles

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

2)In C + +, it is a compiler error to make the common pointer point to const variable, but it is allowed in C.

 

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

3)In C language, void pointer can be directly assigned to other pointers, such as int *, char *. But in C + +, you must explicitly type the void pointer.

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

We’ll notice this when we use malloc(). The return type of malloc() is void *. In C + +, we must explicitly convert the return value type of malloc() to an appropriate type, such as “int * P = (int *) malloc (sizeof (int))”. In C, type conversion is not required.

4)The following programs can be compiled and run normally in C, but they fail to compile in C + +. The const variable in C + + must be initialized, but it is not necessary in C. Thank pravasi meet for making this point.

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

Line 4 [error] uninitialized const’a ‘[- fpermissive]

5)That’s the worst answer, but it’s still valid. We can use one of the C + + specific keywords as the variable name. The program will not be compiled in C + +, but will be compiled in C.

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

Similarly, we can use other keywords, such as delete, explicit, class, etc.

6)C + + does more strict type checking than C. For example, the following program is compiled in C, but not in C + +. In C + +, we get compiler error “invalid conversion from ‘Int’ to ‘char *”. Thank pravasi meet for adding this.

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

7)C + + requires the main return ‘Int’ type

Notes on C / C + + Programming: write C programs that can't be compiled in C + +, I'll teach you!

 

Hope to help you! More programming knowledge, will continue to share Oh~

In addition, if you want to better improve your programming ability, learn C language c + + programming! Overtaking on the curve, one step faster! I may be able to help you here~

C language c + + programming learning circle,QQ group 1090842465Click to enterWeChat official account: C language programming learning base

Share (source code, project video, project notes, basic introductory course)

Welcome to change and learn programming partners, use more information to learn and grow faster than their own thinking Oh!

Programming learning book sharing:

Programming learning video sharing: