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

David Schwartz (davids@webmaster.com)
Tue, 14 Apr 1998 14:53:13 -0400


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

Because of the semantics of what a semicolon after it means. Consider:

#define FOO1(a) do { baz(a); bar(); } while(0)
#define FOO2(a) { baz(a); bar(); }

if(x==1) FOO1(1); else FOO1(1);
if(x==1) FOO2(1); else FOO2(2);

See? Both look like they should work, but only FOO1 actually does.

DS

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