Chapter 6 pre-processor

Source: Internet
Author: User

 

Today, I spent some time reading chapter 6.

 

1.Spaces in macro definition cannot be ignored.

 

Compared with macro calls, macro definitions are "hidden organs ". For example, the following code:

 

# Define f (x )(
(X)-1)

 

There are two answers:

 

1. f (x)
Represents (x)-1)

2. f
Represents (x)-1)

 

The correct answer is the second one, because there is a space between F and (x! This rule is not applicable to macro calls, but only

 

In macro definition, that is, F (3) and F (3) have the same results.

 

2.Macros are not functions.

 

Because macros seem to behave very similar to functions on the surface, Programmers sometimes can't help but regard the two

 

Equivalent. For example, the following code:

 

# Define ABS (x)> 0? (X):-(x ))

 

If it is called in the form of ABS (a-B) and the parameter is not enclosed in brackets, the problem will be found, so to avoid

 

Parameters and results must be included in these problems.

 

In macro definition, if a parameter is used multiple times, this parameter will be evaluated multiple times. Therefore

 

Code (some code ):

 

# Define max (A, B) (a)> (B )? (A): (B)
)

 

Biggest = x [0];

 

I = 1;

 

While (I <n)

Biggest
= Max (biggest, X [I ++]);

 

The above code is not only inefficient, but also produces errors. After expansion, you will find that X [I ++] will be evaluated twice to export

 

Error. If Max is a function, there will be no such problems. To solve these problems, we need to ensure that the macro definition

 

So we need to change the while loop to the for loop. Or use functions or code.

 

Another danger of using macros is that after macro expansion, a very large expression may be generated, occupying much more space than coding.

 

The space that the user expects. It is also the macro definition of Max. If we compare the four numbers max (A, max (B, max (c, d )));,

 

You will find that the expanded code is too long!

 

3.Macro is not a statement

 

Sometimes programmers try to define macro behaviors similar to statements, but the results are surprising.

 

# Define a (x) if (! X) assert_error (_ file _, _ line _)

 

If (x> 0 & Y> 0)

 

A (x> Y );

 

Else

 

A (Y> X );

 

_ File _ and _ line _ are built-in. After you expand and sort out the code, you will find that this is a "suspension" problem. Someone will say

 

We add parentheses in the macro definition to enclose sentences, so you will find another problem. There will be one more question before else.

 

Semicolon. This is a syntax error. However, if you remove the semicolon from the code, you will find that the code is somewhat "weird ".

 

4.Macro is not a Type Definition

 

Another purpose of macros is to make the types of multiple variables declared in one place, which is equivalent to the type definition. For example:

 

# Define T int

 

T;

 

T B, C;

 

This method has an advantage-portability. But it is best to use the type definition, because this method also

 

Yes.

 

# Define T1 int *

 

Typedef int * t2;

 

T1 A, B;

 

T2 C, D;

 

After the first statement is expanded, you will find that A is declared as a pointer, while B is an integer variable, while the second one is

 

Declare two pointer variables.

 

This is Chapter 6.

 

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.