C language macro definition use Analysis _c language

Source: Internet
Author: User
Tags abs

1. How do I differentiate between macro names and macro strings in a macro definition? What should I pay attention to for macros with parameters?

In a macro definition, the macro name and macro string are distinguished by spaces. When the compiler is working on a macro definition, first read 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 for the bank are "macro strings." Diagram: #define + N Spaces (1 < N) + macro name (no spaces in the middle) + N spaces (1 < N) + macro string (until the bank ends). This is all about a single line of macro definitions, and if you cross multiple lines, you can use the "\" character to "continue", which is essentially a line to treat.

There can be no spaces between the macro name and "()" for "with parameter macros", otherwise it becomes "parameterless macro" (according to the above principle). And when the name "no parameter Macro" and "with parameter macro" are the same, "No parameter macro" will block "with parameter macros", even if it is called "with parameter macros", it won't work.

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" respectively. G (4) "is replaced by" (X) (2*x) (4); when "F (4)" is invoked, the system is not replaced with "2*4". Instead, it replaces "123 (4)", stating that "#define F 123" completely masks "#define F (x)" (2*x), and that "#define F (x) (2*x)" After the annotation "#define F 123" can work correctly. Therefore, when you make a macro definition, you should pay close attention to the effect of the space, and the names of the "with parameter macros" and "parameterless macros" must not be the same, otherwise there will be confusion. However, the effect is not affected by the call to the space-time grid in the macro, for example, F (3) and F (3) have the same effect (f (X) is with the parameter macro).

2, macros and functions in the use of the way and the effect of how similarities and differences?

When you define a macro, you should be good at using parentheses to encapsulate variables, enclose each parameter, prevent problems with precedence, and enclose the entire result expression to prevent problems when a macro is used for a more complex expression. Try to improve the reliability of the macros. For example: "#define ABS (x) (((x) > 0)? (x): (x) "Reliability is much better than" #define ABS (x) x > 0? X:-X "can be tested with ABS (A-B)."

In the case of a macro call, it is important to note that if you have a self-increasing (+ +) or self-subtraction (-) operator, you are likely to have side effects. Because when a macro is replaced, if the variable appears more than once, is equivalent to the self-increase or self-reduction operation into many times, this is completely different from the function call, the function call in the form of the participants copy the value of the argument, and the parameter operation does not affect the argument, and the macro call is to modify the argument directly. For example: a = 5; "ABS (a++)" ((a++) > 0) after the expansion? (a++): (-a++) ", after the operation completes" a = 7 "instead of" a = 6 "; When you write a function, you don't have to worry about it at all.

If multiple nested calls are made when a macro is invoked, the macro expands to produce a very large expression and is quite complex; this is not the case for a function call.

3, macro and type definitions the difference between typedef

Because the essence of a macro is a substitution, you can encapsulate the variable type, using the package as a variable definition, the advantage of this is to increase portability, when the change only need to change the macro definition. For example:

Copy Code code as follows:

My_type A;
My_type b,c,d;

But it's best not to do that because we have a typedef that is specifically type-defined. Also, using type definitions makes the code more generic and avoids some deep-seated problems. For example:
Copy Code code as follows:

typedef UINT_8 * MY_TYPE2

My_type1 a,b;
My_type2 c,d;
Analysis

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

Copy Code code as follows:

Uint_8 *a,b;
My_type2 c,d; Because My_type2 is already a type.

As you can see, you originally wanted to define the two pointer variable a,b; now it's turned into a pointer variable A and an integer variable B, which is 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, decisively choose "typedef" Discard macro definition, otherwise the disadvantage is their own.

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.