OFFTOPIC: 'do { ... } while (0)?'

C. Scott Ananian (cananian@lcs.mit.edu)
Tue, 14 Apr 1998 19:23:45 -0400 (EDT)


On Tue, 14 Apr 1998 clove1!mike@uunet.uu.net (Mike Howard) wrote:

> Why are so many macros written as: do { ... } while (0) rather than
> as simply { ... } ?

So that
if (condition)
MACRO();
else
foo;

works properly. There are other corner cases. Technically speaking, a
{}-macro expands to a BLOCK while the do{}while(0) expands to a STATEMENT.
The grammar for the C language treats these differently, and we usually
expect a macro to be a statement, not a block.
--Scott
@ @
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-oOO-(_)-OOo-=-=-=-=-=
C. Scott Ananian: cananian@lcs.mit.edu / Declare the Truth boldly and
Laboratory for Computer Science/Crypto / without hindrance.
Massachusetts Institute of Technology /META-PARRESIAS AKOLUTOS:Acts 28:31
-.-. .-.. .. ..-. ..-. --- .-. -.. ... -.-. --- - - .- -. .- -. .. .- -.
PGP key available via finger and from http://www.pdos.lcs.mit.edu/~cananian

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu