Macro definition of C language notes

Source: Internet
Author: User
Tags naming convention

(a) Symbolic constants

A macro definition is a substitution policy in C that uses a preprocessing command #define equate a string (lengthy) text with a name (called a macro) and then use the macro in bulk in the source code. In the preprocessing phase, replace the macros in the source code with the original text. For example, in the source code:

#define  PI   3.14
Then in the next code, you need to write 3.14 of the place can be directly replaced by PI. When preprocessing, PI is changed back to 3.14.

What good is it to change? In case the code in the 3.14 need to change all to 3.1415926, then if there is no previous macro definition, you can only modify, but with the macro definition, just modify the macro definition is good:

#define  PI   3.1415926


As you can see, each # define command consists of three parts, namely:#define命令本身; abbreviations (macros); Replace text or bodies. Each part is separated by a space, so there cannot be spaces in the macro name, and the C variable naming convention must be followed. Macro names generally use uppercase letters.

In preprocessing, the process of changing from a macro back to a body becomes a macro expansion. Some macro-defined bodies are longer, and you can use backslashes "\" at the end of a line to extend the remainder to the next line (but note that if the second row is not left-aligned, then the whitespace that begins is also considered part of the body).


The body part can be a constant or a C expression, or even a complete statement (that is, with a semicolon), which can be any string. It is important to note that if the body still contains a macro name, the macro will be replaced, but if the macro name is enclosed in double quotes in the body, then the macro substitution will not occur and will only be interpreted literally .


(b) Class function macros

There are two types of macro definitions, all of which are non-parametric, called Class-object macros, which are also known as symbolic constants, and another parameter called a class function macro. For example:

#define FUN (x)  x * x
The shape of a class function macro is similar to a function, followed by a pair of parentheses followed by a name, and the argument list is enclosed in parentheses. Then its subject is some sort of arithmetic rule for these parameters. When using this macro, X can be replaced by other characters, just like the parameters of the function, and x is also the function of the parameter.
This code in the code:

x = Fun (4);
will be replaced by:

x= 4 * 4;
May be a bit around, but I personally put this macro into a two-step process of understanding: First, the Fun (4) directly replaced by x * x, and then the "argument" 4 instead of the "parameter" X (do not know whether the machine is so executed, will it be my innovation?) )。


However, you should be wary of class function macros: when the preprocessor is expanded, it simply replaces the text instead of the function's arguments . For example, the macro just used this way:

x = Fun (3 + 4);
We expect to replace it with:
x = 7 * 7;
Can actually be:

x = 3 + 4 * 3 + 4;
We don't get the results we want because of the binding sequence. The way to avoid this result is to:when defining a class function macro, it is a good idea to enclose each parameter that appears in the body with parentheses, and also to protect each operation rule with parentheses .。 For example, fun should be defined like this:

#define FUN (x)  ((x) * (x))
So that's what happens after the substitution:

x = ((3 + 4) * (3 + 4))
This may seem troublesome, but it is a good way to avoid accidents.

The following is the handling time (people write too wonderful, I would be ashamed to steal lazy ~, from the "Linux C one-stop programming")
Functional macro definitions are often written in this form (taken from kernel code include/linux/pm.h): #define DEVICE_INIT_WAKEUP (Dev,val) do {device_can_wakeup (dev) =!! (Val); Device_set_wakeup_enable (Dev,val); } while (0) Why should I enclose it in do {...} while (0)? What's wrong with not enclosing it? #define DEVICE_INIT_WAKEUP (Dev,val) device_can_wakeup (dev) =!! (Val); Device_set_wakeup_enable (Dev,val); if (n > 0) device_init_wakeup (d, v); After the macro expands, the second statement of the function body is not in the IF condition. So simply use {...} to make up a block of statements. #define DEVICE_INIT_WAKEUP (Dev,val) {device_can_wakeup (dev) =!! (Val); Device_set_wakeup_enable (Dev,val); }if (n > 0) device_init_wakeup (d, v); else
Continue; The problem is device_init_wakeup (d, v); The end of; If it is not allowed to write this; No, it doesn't look like a function call, but if you write this; After the macro expands, there is a syntax error, and if statement is this; The number has ended and cannot be paired with the else. Therefore, do {...} while (0) is a better solution.
And then I do some additions, do {...} while (0) How exactly does this form work? We know that this do...while () loop is called the exit condition Loop, and the judging condition is checked after the execution of the loop, so that the statement in the loop body is executed at least once, and the while condition is set to 0, which means that the loop body happens only once, which is a clever way!!


(c) #与 # #运算符

Write here really a little can't hold, sister's C language is also really profound, a macro definition to make so complex ...

As mentioned above, the macro name within the double quotation marks in the body is not replaced but is treated as plain text, so what if it has to be replaced? (how can there be such a two demand?) The way to do this is to precede the macro name with a # symbol, which is called: string (stringizing).

Note: This symbol is only used for parameters in class function macros, that is, this usage does not work in class object macros (I have tried, it is true).


# #运算符把两个语言符号组合成单个语言符号, but it can also be used as a replacement part of a class object macro, known as a pre-processor binder. In my opinion, it is used to name objects (variables or functions), for example, we often give variables such as X1 x2 x3 Such names, which are characterized by a part of the same and the other part. Chestnuts:

#define XNAME (n)   x # # N
And then

<span style= "FONT-SIZE:18PX;" >int XNAME (1) = 14;</span>
will be replaced by:

int  x1 = 14:

Just the sauce, all finished.


Macro definition of C language notes

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.