Interview Questions

What's the best way to write a multi-statement macro?

C Interview Questions and Answers


(Continued from previous question...)

What's the best way to write a multi-statement macro?

The usual goal is to be able to invoke the macro as if it were an expression statement consisting of a function call:
MACRO(arg1, arg2);
This means that the ``caller'' will be supplying the final semicolon, so the macro body should not. The macro body cannot therefore be a simple brace-enclosed compound statement, because of the possibility that the macro could be used as the if branch of an if/else statement with an explicit else clause:
if(cond)
MACRO(arg1, arg2);
else /* some other code */

If the macro expanded to a simple compound statement, the final, caller-supplied semicolon would be a syntax error:
if(cond)
{stmt1; stmt2;};
else /* some other code */

The traditional solution, therefore, is to use
#define MACRO(arg1, arg2) do { \
/* declarations */ \
stmt1; \
stmt2; \
/* ... */ \
} while(0) /* (no trailing ; ) */

When the caller appends a semicolon, this expansion becomes a single statement regardless of context. (An optimizing compiler will remove any ``dead'' tests or branches on the constant condition 0, although lint may complain.)
(Another possibility might be
#define MACRO(arg1, arg2) if(1) { \
stmt1; \
stmt2; \
} else

but it is inferior, since it quietly breaks the surrounding code if the caller happens to forget to append the semicolon upon invocation.)
If all of the statements in the intended macro are simple expressions, with no declarations or loops, another technique is to write a single, parenthesized expression using one or more comma operators:
#define FUNC(arg1, arg2) (expr1, expr2, expr3)
This technique also allows a value (in this case, expr3) to be ``returned.''
Some compilers, e.g. gcc, are also able to expand compact functions in-line, either automatically or at the programmer's request (perhaps with a nonstandard ``inline'' keyword or other extension).

(Continued on next question...)

Other Interview Questions