1. How do I distinguish "macro name" from "macro string" in macro definition "? What should I pay attention to for macros with parameters?
In macro definition, "macro name" and "macro string" are distinguished by "space. During macro definition processing, the compiler first reads the string from the first space after "# define" until the next space is met. The string between the two spaces is the macro name ", after "macro name" is determined, all other strings in this line are "macro strings ". Illustration: # define + N spaces (1 <N) + macro name (no space in the middle) + N spaces (1 <N) + macro string (until the end of the line ). Here we will talk about macro definitions within a row. If there are multiple rows, we will use the "\" character to "continue". In essence, we can treat it as a row.
For "macro with Parameters", there cannot be spaces between the macro name and "()"; otherwise, it becomes "non-parameter macro" (according to the above principles ). In addition, when the names of "no-parameter macro" and "with-parameter macro" are the same, "No-parameter macro" will block "with-parameter macro ", it does not work even if it is called in the form of "with macro parameters.
Test content and results:
Experiment Analysis:
We can see that "# define PI 3.1415" and "# define p I 689" are two different macro definitions: "PI" and "P"; "G (4) "replaced by" (X) (2 * X) (4) ". When" F (4) "is called, the system is not replaced with" 2*4 ", instead, it is replaced with "123 (4)", indicating that "# define F 123" completely blocks "# define F (X) (2 * X )", after "# define F 123" is commented out, "# define F (X) (2 * X)" can work normally. Therefore, when defining macros, pay close attention to the influence of spaces, and the names of "with parameter macros" and "without parameter macros" must not be the same; otherwise, confusion may occur. However, calling the time space lattice in a macro does not affect the effect. For example, F (3) and F (3) have the same effect (F (X) is a macro with parameters ).
2. What are the similarities and differences between macro and function usage and effects?
In macro definition, we should be good at encapsulating variables with parentheses, including every parameter to prevent priority-related problems. At the same time, the entire result expression should also be included, to prevent problems when macros are used in a more complex expression. Try to improve the reliability of Gao Hong ,. For example, "# define ABS (x)> 0 )? (X): (-x) "is much more reliable than" # define ABS (x) x> 0? X:-x ", which can be tested with ABS (a-B.
When calling a macro, if you have an auto-increment (++) or auto-increment (--) operator, you must note that it may produce side effects. When a macro is replaced, if the variable appears multiple times, it is equivalent to the auto-increment or auto-increment operation multiple times, which is totally different from the function call, in a function call, the form parameter copies the value of the real parameter, and the operation on the form parameter does not affect the real parameter, and the macro call directly modifies the real parameter multiple times. For example, after a = 5; "ABS (a ++)" is expanded, it becomes "(a ++)> 0 )? (A ++): (-a ++) ". After the operation is complete," a = 7 "instead of" a = 6 ". You don't have to worry about this issue when writing a function.
If a multi-layer nested call is performed during macro calls, a very large expression is generated after macro expansion, which is quite complex; function calls do not.
3. Differences between macro and type definition typedef
Because the essence of macros is replacement, a variable type can be encapsulated to define variables. The advantage of doing so is to increase portability, you only need to modify the macro definition. For example:
Copy codeThe Code is as follows: MY_TYPE;
MY_TYPE B, c, d;
But it is better not to use it this way, because we have typedef which specifically defines the type. Moreover, the use of the type definition will make the code more generic and avoid some deep problems. For example:Copy codeThe Code is as follows: typedef uint_8 * MY_TYPE2
MY_TYPE1 a, B;
MY_TYPE2 c, d;
Analysis:
In terms of concept, both MY_TYPE1 and MY_TYPE2 are identical and are pointers to uint_8. However, when we declare multiple variables, a problem occurs. They are expanded:
Copy codeThe Code is as follows: uint_8 * a, B;
MY_TYPE2 c, d; // because MY_TYPE2 is already a type
We can see that we wanted to define two pointer variables a and B, but now it is a pointer variable a and an integer variable B, which is not what we want. MY_TYPE2 itself is a type (custom), So c and d are pointer types, as expected. Therefore, if you want to customize the type, choose "typedef" to discard the macro definition. Otherwise, you will lose yourself.