[E liangshi Yiyou network ?] There may be some problems when writing macros. For example, the general goal is to write a macro containing a separate function call statement. This means that the caller needs to provide the final semicolon, while the macro does not. Therefore, the macro cannot be a compound statement enclosed by an arc. If this happens, a syntax error occurs during the call, just as if an else clause is added to the IF branch of the IF/else statement.
Therefore, I personally think that the traditional ending scheme is used like this:
# Define macro (arg1, arg2) do {\
/* Declarations */\
Stmt1 ;\
Stmt2 ;\
/*...*/\
} While (0)/* no ending ;*/
When the caller adds a semicolon, the macro is extended to a separate statement under any circumstances. The optimized compiler removes "invalid" tests or branches with a condition of 0, although lint may warn.
If the statements in the macro body are simple statements without declaration or loop, another technique is to write a separate expression separated by one or more comma operators. This technique can also return a value.
Want to know moreProgramming Language tutorial setYou can log on to e liangshi Yiyou.
An Effective Method for writing macros