Only when learning about single-chip microcomputers did I really know what the C language is and what it is used for~ However, the use of C language in embedded systems is only a small part of it. There are many other places for its application. Haha, we will not discuss this here. When we write programs, do we make a lot of mistakes and even if the program is compiled, it still cannot achieve the expected results? Then it is difficult for us to find the mistakes, right? I think the reason why a language can be called a language is that it is a tool, a tool for mutual communication and conveying each other's intentions. As a language, it must have its own grammar. If you want to communicate with each other, you must first learn its grammar well. (For example, expressions, functions, loops, pointers) I call it the grammar of the C language. Although C++ is very powerful, it also has many pitfalls. So I have two purposes for this blog post. One is to summarize some of the misuse and mistakes of C++, and the other is to summarize some basic grammar of C++. The first time: 1. About self-increment and self-decrement (that is, ++i, i++) To add or subtract one to a number, we can: i += 1;j -= 1;The C language also allows the ++ and -- operators. This is actually misleading because ++ and -- can be used as prefixes and suffixes, which may change the value of the operands. Let's take a look:i = 1;printf("i is %d\n",++i); /* prints i is 2 */printf("i is %d\n",i++); /* prints i is 2 */The result of the calculation expression i++ is i, but it will cause i to be incremented: i = 1; printf("i is %d\n",i++); /* prints i is 1/ */printf("i is %d\n",i); /* prints i is 2 */ The firstprintf showsi's original value before the increment, the secondprintf showsi's new value after the change;Of course-- I won't give examples for similar ones~but using ++ and -- multiple times in the same expression is often difficult to understand. Let's look at the following example: i = 1; j = 2; k = ++i + j++; the final values of i, j, k are 2, 3,4and++iis2 j++is2; In summary: whether it is ++i or i++, after executing this statement, the value ofi is increased by 1, but the value of(++i) is increased by 1, while(i++) #define 2.1.typedef In addition to directly using standard type names (such as int, char, float, double) and self-declared structures, unions, pointers, and enumeration types, the C language can also use typedef to declare new type names to replace existing type names. typedef unsigned char u8; typedef unsigned int u16; u8 count; u16 time; typedef struct { u8 month; u8 day; u16 year; }DATE; DATE brithday; To summarize, the method of declaring a new type name is: 1.First write the definition body according to the method of defining variables(For exampleunsigned int i) 2.Replace the variable name with the new variable name(For example, replacei withu16) 3.Addtypedef (typedef unsigned int u16) at the very beginning4.Then define the variable with the new type name 2.2 #define 2.1.1Macro definition without parameters #define Identifier string #define PI 3.1415926 Note: 1.Its function is to use the specified identifierPI to replace3.1415926 2.The macro definition is to use the macro to replace the string, that is, to do a simple replacement, without correctness check. If written as #define PI 3.l4l6926 That is,1 is written as the letterl, but the preprocessor is substituted as usual without any syntax check!! 2.1.2Macro definition with parameters #define Macro name(Parameter) String #define S(a,b) a*b area = S(a,b); #define MAX(x,y) (x)>(y) ? (x)y)3.The difference between typedef and #defineGenerally speaking, typedef because it can handle pointer types correctlytypedef char *String1;#define String2 char *String1 s1,s2;String2 s3,s4;s1,s2,s3 are defined as char*, but s4 is defined as char type 3. static variables static variables can be roughly divided into three usages 1. Used in local variables, becoming static local variables. Static local variables have two uses,memory function and global lifetime. 2. Used for global variables,The main function is to limit this global variable from being called by other files. 3. Used for members in a class.Indicates that this member belongs to this class but not to any specific object in the class 1. Static local variables Static local variables belong to the static storage mode, which has the following characteristics: (1) The lifetime of a static local variable defined in a function is the entire source program, but its scope is the same as that of an automatic variable. The variable can only be used in the function that defines it. After exiting the function, although the variable continues to exist, it cannot be used. (2) It is allowed to assign initial values to static local variables of a constructed class, such as arrays. If no initial value is assigned, the system automatically assigns a value of 0. (3) If no initial value is assigned to a static local variable of a basic type when it is declared, the system automatically assigns a value of 0. If no initial value is assigned to an automatic variable, its value is indeterminate. According to the characteristics of static local variables, it can be seen that it is a quantity with a lifetime of the entire source program. Although it cannot be used after leaving the function that defines it, it can continue to be used when the function that defines it is called again, and the value left after the previous call is saved. Therefore, when a function is called multiple times and the values of certain variables are required to be retained between calls, static local variables can be considered. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is still better to use local static variables. For example: void fun(){static int a = 1;a++;}When entering this function for the first time, the variable a is initialized to 1! and then incremented by 1. Each time you enter this function, a will not be initialized again, and only the operation of self-increment by 1 will be performed; Before the invention of static, to achieve the same function, only global variables can be used: int a = 1; void fun() { a++; } 2.Adding static before static global variables (external variables) constitutes a static global variable. Global variables are static storage methods, and static global variables are also static storage methods. There is no difference between the two in terms of storage methods. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. Static global variables limit their scope, that is, they are only valid in the source file that defines the variable, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file, they can only be used by functions in the source file, so errors in other source files can be avoided. From the above analysis, we can see that changing a local variable to a static variable changes its storage method, that is, changes its lifetime. Changing a global variable to a static variable changes its scope, limiting its scope of use. Therefore, the static specifier plays different roles in different places. 3.staticclass member variables The static keyword has two meanings,You should judge based on the context 1.Indicates that the variable is a static storage variable, indicating that the variable is stored in the static storage area. 2. indicates that the variable is an internal link ( This situation means that the variable is not within any {}, just like a global variable , at this time add static), that is to say in other .cpp files , the variable is not visible ( you can't use ). 2. static functions——internal functions and external functions When a source program consists of multiple source files, Clanguage divides functions into internal functions and external functions according to whether they can be called by functions in other source files. 1 [/font=宋体]Internal functions (also called static functions ) If a function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program, this function is called an internal function. To define an internal function, just add a “static” keyword before the function type, as shown below: static Function type Function name (Function parameter list) {…} The keyword “static” means “static” in Chinese, so internal functions are also called static functions. But here "static" does not refer to the storage method, but means that the scope of the function is limited to this file. The advantage of using internal functions is that when different people write different functions, they don't have to worry about whether the functions they define have the same name as functions in other files, because it doesn't matter if they have the same name. 2 External functions Definition of external functions: When defining a function, if you do not add the keyword "static", or prefix it with the keyword "extern", it means that this function is an external function: extern] Function typeFunction name(Function parameter table) {……} When calling an external function, you need to explain it: [extern] Function typeFunction name(Parameter type table)[, Function name2(Parameter type table2)……];
|