f78
Often it is desirable to define a macro that expands into a compound statement. Consider, for example, the following macro, that advances a pointer (the argument, p, says where to find it) across whitespace characters.
#define SKIP_SPACES (p, limit) \ { register char *lim = (limit); \ while (p != lim) { \ if (*p++ != ’ ’) { \ p--; break; }}}Note:
SKIP_SPACES (p, lim);But this can cause trouble before else statements, because the semicolon is actually a null statement. Suppose you have the following input.
if (*p != 0) SKIP_SPACES (p, lim); else. . .The presence of two statements—the compound statement and a null statement—in between the if condition and the else makes invalid C code. The definition of the macro SKIP_SPACES can b 4d2 e altered to solve this problem, using a do...while statement. Use the following input as an example.
#define SKIP_SPACES (p, limit) \ do { register char *lim = (limit); \ while (p != lim) { \ if (*p++ != ’ ’) { \ p--; break; }}} \ while (0)Now SKIP_SPACES (p, lim); expands into one output statement as the following example shows.
do { : : : } while (0);0