Reprint--C Language macro definition (2)

Source: Internet
Author: User

Solo wind

Links: C Language macro definitions (2)

1. How do I distinguish between macro names and macro strings in a macro definition? What should be noted for macros with parameters?

In the macro definition, the macro name and macro string are distinguished by a "space", in which a space can have a significant effect on the result of the extension. When the compiler processes the macro definition, first reads the string from the first space after "#define", until the next space is met, the string between two spaces is "macro name", and after the "macro name" is determined, all other strings of our bank are "macro strings". Illustration: #define + N Spaces (1 < N) + macro name (no spaces in middle) + N spaces (1 < N) + macro string (until end of bank). This is all about a single line of macro definition, if the "\" character is used to "continue" in a row, it can essentially be treated as a line.

There can be no space between the macro name and () for the parameter macro, otherwise it becomes a parameterless macro (based on the principle above). Also, when the name "parameterless macro" and "parametric macro" are the same, the "parameterless macro" will mask the "parametric macro", even if it is called with the "Parametric macro" method.

Test contents and Results:

Experimental Analysis:

You can see that "#define PI 3.1415" and "#define p I 689" are two different macro definitions "PI" and "P", "G (4)" is replaced by "(X) (2*x) (4)", When "F (4)" is called, the system is not replaced with "2*4", Instead of "123 (4)", the "#define F 123" is completely shielded from "#define F (x) (2*x)", and "#define F (x) (2*x)" After commenting out "#define F 123" can work correctly. Therefore, when you make a macro definition, you should pay close attention to the effect of spaces, and the names of "parametric macros" and "parameterless macros" must not be the same, or there will be confusion. However, when a macro calls a space-time lattice, it does not affect the effect, such as f (3) and F (3) Effects (f (X) is a parametric macro). Therefore, in the actual development, the macro such a preprocessor, should only be used in moderation, do not give it too much functionality, and in C + + added a const variable, how much to make the macro lost some useful.

2. What are the similarities and differences between macros and functions in terms of usage and effect?

In macro definition, be good at using parentheses to encapsulate variables, enclose each parameter, prevent the occurrence of priority-related problems, and the entire result expression should be enclosed to prevent problems when the macro is used in a more complex expression. Try to improve the reliability of the macro. For example: "#define ABS (x) (((x) > 0)? (x): (-X) "is far more reliable than" #define ABS (x) x > 0? x: × "can be tested with ABS (A-B).

When a macro is called, it is important to note that if you have the self-increment (+ +) or the decrement (--) operator, you are likely to have side effects. Because when a macro is replaced, if the variable appears more than once, it is equivalent to a self-increment or decrement operation more than one time, this is completely different from the function call, the function call in the form of the value of the copy argument, and the operation of the parameter does not affect the actual parameters, and the macro call is to modify the arguments directly. For example: a = 5; "ABS (a++)" Expands and Becomes "(((a++) > 0)? (a++): (-a++)) ", after the operation is complete" a = 7 "instead of" a = 6 "; When written as a function, you don't have to worry about it at all.

If a multi-layered nested call is made when a macro is called, the macro expands to produce a very large expression and is rather complex, which is not the case with function calls.

3. Macros and types define the difference between typedef

Since the essence of a macro is substitution, you can encapsulate the variable type in a single layer and use the package as a variable definition, the benefit of which is to increase portability and only change the macro definition when modifying. For example:

[CPP]View Plaincopyprint?
    1. #define My_type Uint_8
    2. My_type A;
    3. My_type b,c,d;

But it's best not to use it because we have a TypeDef, which is specifically for type definition. Also, using type definitions makes the code more generic and avoids some deep-seated problems. For example:

[CPP]View Plaincopyprint?
    1. #define MY_TYPE1 Uint_8 *
    2. typedef UINT_8 * MY_TYPE2
    3. My_type1 A, B;
    4. My_type2 c,d;

Analysis:

Conceptually, My_type1 and my_type2 are exactly the same, all pointers to uint_8, but when we declare multiple variables, there is a problem. They are expanded into:

[CPP]View Plaincopyprint?
    1. Uint_8 *a,b;
    2. My_type2 c,d; //Because my_type2 is already a type of

As you can see, you would have wanted to define two pointer variables, a-B; now it's a pointer variable A and an integer variable, and that's not what we want. And my_type2 itself is a type (custom), so c,d are pointer types, in line with expectations. Therefore, if you want to customize the type, decisive choice "typedef" to abandon the macro definition, otherwise the disadvantage is their own.

There are so many traps in the macro definition, so it is best to use it to name the constants in practice, even if you do some simple operations, and you have to decide whether or not an exception will occur, especially if the macro is nested (although it is not recommended).

Reprint--C Language macro definition (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.