Multiple-statement macros
utzoo!decvax!duke!unc!tim
utzoo!decvax!duke!unc!tim
Sun Jan 9 21:35:13 AEST 1983
This controversy seems to have gotten revived on unix-
wizards. No one has mentioned what seems to me the most
important aspect of all these examples: doing something
like:
#define groucho { a++; b = c * d; }
if (cond) groucho else a--;
is terrible style. If a macro does something, make it look
like a function call, even if it doesn't have any parame-
ters. Parameterless macros should be used only for defining
symbolic constants. C can be hard enough to read without
abusing the preprocessor.
In any case, all multiple statement macros should use
the , (comma) operator. This will only fail if you use con-
trol structures, which is bad practice in a macro anyway.
(There is always ?: if you need a condition.) This makes the
whole thing one statement, eliminating any problem.
Tim Maroney
unc!tim
More information about the Comp.lang.c
mailing list