Several encapsulation methods of function macros in C language

Several encapsulation methods of function macros in C language

In C language development, in addition to using functions to encapsulate code, macros are also often used to encapsulate some important or concise code.

There are three types of macros in C development: predefined macros, macros without parameters, and macros with parameters. Usually, macros with parameters are also called function macros. Function macros are macro definitions that contain multiple statements. They are usually a frequently used macro. The statement encapsulation of the called function, and we do not want to reduce the additional overhead of popping and pushing the stack through functional encapsulation. In actual project development, function macros are very powerful. Here are three commonly used encapsulation methods:

The first way: {} method

for example:

At this time, if called in a non-control statement, it can be compiled normally, as follows:

But when called in a control statement, such as a branch statement if (else if, else, etc.) such as:

The compiler will report an error as follows:

The above statement expands to:

SWAP(x, y); The following ; terminates the scope of if, and the subsequent else will of course not find a matching if. Macro functions should work with any syntax.

Advantages and disadvantages of this function macro:

Advantages: simple and crude.

Disadvantages: It cannot be called directly in an if statement without curly braces and with branches; it can be called directly without ;.

The second do{...}while(0) method

do{...}while(0) is a control flow statement, which is a compound statement. Parameter detection can be added to the statement block. For example:

The compiler will treat do{...}while(0); as a statement. Therefore, the do{...}while(0) function macro can be called directly in an if statement without braces and with branches. For example:

Advantages and disadvantages of this function macro:

Advantages: Supports direct calls in if statements without curly braces and branches; supports early exit of function macros; must use ; when forcing calls.

Disadvantages: No return value, cannot be used as the rvalue of an expression.

Third ({}) way

({}) is the GNU C extended syntax, which is the native syntax of non-C language. The encapsulated form is as follows:

({}) can be used both in branch statements and as rvalues, for example:

Advantages and disadvantages of this function macro:

Advantages: Supports direct calls in if statements without curly braces and branches; has return values ​​and supports rvalues ​​as expressions.

Disadvantages: Early exit of function macros is not supported; non-C native syntax may not be supported by the compiler.

Summarize

To sum up, among the three function macro encapsulation methods {}, do{...}while(0) and ({}), {} should be avoided as much as possible, and do{ is generally chosen to consider compatibility. ..}while(0), when you need a function macro to return, you can consider using ({}) or directly defining the function.

Guess you like

Origin blog.csdn.net/weixin_44059661/article/details/134177914