Follow the Embedded Learning Station for more fresh hot topics every day.
🤟Note: This article has a total of 4760 words, estimated reading time is 15 minutes~
1. Introduction
To a large extent, C++ is a superset of C, which means that an effective C program is also a valid C++ program.
The main difference between C and C++ is that:C++ supports many additional features. However, there are many rules in C++ that differ slightly from C. These differences can cause a C program to behave differently or not run at all when compiled as a C++ program.If C code is compiled as a C++ program, it may lead to erroneous messages.
The release of the C99 standard complicates matters further, as in some cases it makes C closer to C++.
For example, the C99 standard allows declarations anywhere in the code and recognizes // comment indicators. In other aspects, C99 increases the differences from C++.
For instance, it introduced variable-length arrays and the restrict keyword. C11 narrowed the differences with C++.
We need to understand the differences between C90, C99, and C11, as well as the differences between C++11 and these standards, and each standard’s differences with the C standard. Of course, C++ is also evolving, so the similarities and differences between C and C++ are constantly changing.
2. Function Prototypes
In C++, function prototypes are essential, but in C they are optional. This difference can be seen when declaring a function with empty parentheses after the function name.
In C, empty parentheses indicate a forward declaration, while in C++, it indicates that the function has no parameters.
That is, in C++, int slice(); and int slice(void); are the same. For example, the following old-style code can be accepted in C, but it will produce an error in C++:
int slice();int main(){ ... slice(20, 50); ...}int slice(int a, int b){ ...}
In C, the compiler assumes that the user is using old-style function declarations. In C++, the compiler assumes that slice() is the same as slice(void), and that the function slice(int, int) has not been declared.
Additionally, C++ allows users to declare multiple functions with the same name, as long as their parameter lists differ.
3. Char Constants
C treats char constants as int type, while C++ treats them as char type. For example, consider the following statement:
char ch = 'A';
In C, the constant ‘A’ is stored in a memory block of int size, more precisely, the character encoding is stored as an int type value. The same numeric value is also stored in the variable ch, but in ch, that value occupies only 1 byte of memory.
In C++, ‘A’ and ch both occupy 1 byte. Their differences will not affect the examples in this book. However, some C programs take advantage of the fact that char constants are treated as int type to represent integer values using characters. For example, if an int is 4 bytes in a system, C code can be written as follows:
int x = 'ABCD'; /* This statement is fine in a C program on a 4-byte int system, but will cause an error in a C++ program */
‘ABCD’ represents a 4-byte int type value, where the first byte stores the character encoding of A, the second byte stores the character encoding of B, and so on. Note that ‘ABCD’ and “ABCD” are different. The former is just a way of writing an int type value, while the latter is a string, which corresponds to an address of a 5-byte memory block. Consider the following code:
int x = 'ABCD';char c = 'ABCD';printf("%d %d %c %c\n", x, 'ABCD', c, 'ABCD');
On our system, the output is as follows:
1094861636 1094861636 D D
This example illustrates that if ‘ABCD’ is treated as an int type, it is a 4-byte integer value. However, if treated as char type, the program only uses the last byte. In our system, attempting to print ‘ABCD’ using the %s conversion specifier would crash the program, as the value of ‘ABCD’ (1094861636) exceeds the range that this type can represent.
The reason this can be done is that C provides a way to set each byte in an int type individually, as each character corresponds to a byte. However, since it relies on a specific character encoding, a better method is to use hexadecimal integer constants, as every two hexadecimal digits correspond to one byte. Chapter 15 has detailed this (the early versions of C did not provide hexadecimal notation, which may have been the reason the multi-character constant technique was developed first).
4. Const Qualifier
In C, global const has external linkage, but in C++, it has internal linkage. That is, the following C++ declaration:
const double PI = 3.14159;
Is equivalent to the following declaration in C:
static const double PI = 3.14159;
Assuming both declarations are outside all functions. The intent of the C++ rule is to make it easier to use const in header files. If a const variable has internal linkage, each file that includes that header will get a backup of the const variable. If a const variable has external linkage, it must be defined in one file and then referenced in other files using the extern keyword.
Incidentally, C++ can use the extern keyword to give a const value external linkage. So both languages can create const variables with internal and external linkage. Their difference lies in which linkage is used by default.
Additionally, in C++, const can be used to declare the size of regular arrays:
const int ARSIZE = 100;double loons[ARSIZE]; /* In C++, this is the same as double loons[100]; */
Of course, the same declaration can be used in C99, but it would create a variable-length array. In C++, const values can be used to initialize other const variables, but this cannot be done in C:
const double RATE = 0.06; // Both C++ and C can do thisconst double STEP = 24.5; // Both C++ and C can do thisconst double LEVEL = RATE * STEP; // C++ can, C cannot
5. Structures and Unions
After declaring a tagged structure or union, you can use this tag as a type name in C++:
struct duo{ int a; int b;};struct duo m; /* Both C and C++ can do this */duo n; /* C cannot do this, C++ can */
The result is that the structure name can conflict with the variable name. For example, the following program can be compiled as a C program, but will fail when compiled as a C++ program, because C++ interprets duo in the printf() statement as a structure type rather than an external variable:
#include<float ("%f\n",="" *="" 0;}<="" 2,="" 4};="" a;="" b;};="" but="" c++="" c,="" code="" duo="" duo);="" fine="" in="" int="" is="" main(void){="" not="" printf="" return="" struct="" this="" y="{" {=""></float>
In both C and C++, you can declare another structure inside a structure:
struct box{ struct point {int x; int y; } upperleft; struct point lowerright;};
In C, you can subsequently use these structures in any way, but in C++, when using nested structures, you must use a special symbol:
struct box ad; /* Both C and C++ can do this */struct point dot; /* C can do this, C++ cannot */box::point dot; /* C cannot do this, C++ can */
6. Enumerations
C++ uses enumerations more strictly than C. In particular, enum constants can only be assigned to enum variables, and then compared with other values. Without explicit type casting, you cannot assign int type values to enum variables, and you cannot increment an enum variable. The following code illustrates these issues:
enum sample {sage, thyme, salt, pepper};enum sample season;season = sage; /* Both C and C++ can do this */season = 2; /* Warning in C, error in C++ */season = (enum sample) 3; /* Both C and C++ can do this*/season++; /* C can do this, error in C++ */
Additionally, in C++, you can declare enum variables without using the enum keyword:
enum sample {sage, thyme, salt, pepper};sample season; /* C++ can do this, C cannot */
Similar to the case of structures and unions, if a variable has the same name as an enum type, it will cause a naming conflict.
7. Pointers to Void
C++ can assign pointers of any type to a pointer to void, similar to C. However, the difference is that you can only assign a pointer to void to other type pointers using explicit type casting. The following code illustrates this:
int ar[5] = {4, 5, 6,7, 8};int * pi;void * pv;pv = ar; /* Both C and C++ can do this */pi = pv; /* C can do this, C++ cannot */pi = (int * ) pv; /* Both C and C++ can do this */
Another difference between C++ and C is that C++ can assign the address of a derived class object to a base class pointer, but C does not have this feature.
8. Boolean Type
In C++, the boolean type is bool, and true and false are both keywords. In C, the boolean type is _Bool, but you must include the stdbool.h header file to use bool, true, and false.
9. Optional Spellings
In C++, you can use or instead of ||, and there are some other optional spellings, all of which are keywords. In C99 and C11, these optional spellings are defined as macros, and you need to include iso646.h to use them.
10. Wide Character Support
In C++, wchar_t is a built-in type, and wchar_t is a keyword. In C99 and C11, the wchar_t type is defined in multiple header files (stddef.h, stdlib.h, wchar.h, wctype.h). Similarly, char16_t and char32_t are both keywords in C++11, but in C11 they are defined in the uchar.h header file.
C++ provides wide character I/O support (wchar_t, char16_t, and char32_t) through the iostream header file, while C99 provides a completely different I/O support package through the wchar.h header file.
11. Complex Type
C++ provides a complex class in the complex header file to support complex types. C has built-in complex types and supports them through the complex.h header file. The two methods are quite different and incompatible. C is more concerned with the needs posed by the numerical computing community.
12. Inline Functions
C99 supports the inline function feature of C++. However, the implementation of C99 is more flexible. In C++, inline functions have internal linkage by default. In C++, if an inline function appears multiple times in multiple files, the definition of that function must be the same and must use the same language symbols.
For example, it is not allowed to use int type parameters in one file’s definition while using int32_t type parameters in another file’s definition. Even if typedef defines int32_t as int, this cannot be done. However, this is allowed in C. C allows mixing inline definitions and external definitions, while C++ does not.
13. Features of C99/C11 Not Present in C++11
Although in the past C could more or less be seen as a subset of C++, the C99 standard introduced some new features that C++ does not have. Below are some features that are only present in C99/C11:
Designated initializers;
Restricted pointers (i.e., restrict pointers);
Variable-length arrays;
Flexible array members;
Macros with a variable number of parameters.


Embedded: 7 Tips for Managing RTOS Memory Performance and Usage

In the autumn recruitment season, why can this position offer an annual salary of 250,000+?

Can you find a substitute for an interview? Relying on “cheating” to get hired, the result was still a big failure…