The macro of C language

Source: Internet
Author: User

The so-called macro is a preprocessing command, what is the deal with it? The necessary conversion processing for the program code before the compilation process. Macros have two functions:

1. The program only needs to change one place when it encounters the need to modify all instances of a particular number of programs that appear in the program.

2. Most C - language function calls bring significant overhead, and macros look like a function without the overhead of a function call.

Although the macro has advantages, but also has its own shortcomings.

The first thing to be clear is simply to replace the macro. For example

#define CHAR char*

Int Main ()

{

Char a= ' a ';

CHAR P1,P2;

p1=&a;

p2=&a;// Error

}

When running the program code, the program will error, what reason? Because the macro is simply replaced, CHAR p1,p2; represented by Char *p1,p2; only P1 is defined as a pointer, and P2 just a normal variable of the character type.

This simple substitution often causes another error to occur:

#define Add (b) A + b

result = Add (3,4) * Add ();

As defined above, the original intent is to calculate the sum of 1 and 2,3 and 4 , and then multiply their results. But the result of the operation is not what we imagined, the compiler would simply replace the expression with the following:1+2*3+4, so we can see where the problem lies. The solution to this problem is not to be stingy with parentheses. Parentheses should be added to each macro parameter and the entire expression to avoid ambiguity.

Second, spaces in the macro definition cannot be ignored. For example:

#define F (x) ((x) * 2)

There are two possible ways to interpret this definition:

① defines the macro F, where f stands for (x) ((x) * 2)

② defines the macro f (x), where x is the macro parameter and thetotalf (x ) macro represents ((x) * 2)

These two definitions can only be the first because there is no space in the middle of the macro definition. With the space first will be the understanding of the macro ambiguity, followed by the compilation will produce unexpected results. Therefore, this situation should be avoided.

The above rules only apply to the definition of macro, there is no space in the middle of the macro call does not matter. i.e. f (5) and

F (5) is the same.

Finally, we want to focus on the side effects of macros.

A macro looks like a function, but it is not a function. Let's look at the following code:

#include <stdio.h>

#define MAX (A, b) a>b?a:b

int main ()

{

Char Ar[3] = {2, 3, 1};

int i = 1; int biggest = ar[0];

while (I < 3)

{

Biggest = Max (biggest, ar[i++]);

}

printf ("Max value=%d\n", biggest);

return 0;

}

The intent of this code is to find the largest value in the array, but after running we found that the result of the operation is not what we imagined 3, but 1, the result is obviously wrong, but where is wrong? Let 's replace Max in the above equation with a macro:

biggest=biggest>ar[i++]?biggest:ar[i++];

First, the variablebiggestwith thear[i++]For comparison, this timeIthe value is1,Ar[1]the value is3, and at this pointbiggestthe value isAr[0]to be2, so the value of the expression is false. here becausei++the side effects, after comparisonIIncrement to2.

Because the result of the relational operation is false,ar[i++]value is assigned to thebiggest, however at this timeIthe value is2.so the value that is actually assigned to the variable isAr[2]that1, at this time, again becausei++the side effects,Ithe value becomes3.Therefore, it is not difficult to understand why the program operation results are not3but1up.

To avoid the side effects of macros, here are two ways to do this:

1. Put forward the i++ , the original Biggest=max (biggest,ar[i++]);

Instead:Biggest=max (Biggest,ar[i]);

i++;

2. change a macro to a function

The macro of C language

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.