Re: fake loop

Thomas Pornin (Thomas.Pornin@ens.fr)
Fri, 3 Aug 2001 18:07:49 +0200


In article <20010803155735.18620.qmail@nwcst31f.netaddress.usa.net> you write:
> #define prepare_to_switch() do { } while(0)

This one is a classic C trick; it is documented in the K&R book. With
this fake loop, the macro can be used like a statement anywhere. For
instance, compare the two following :

#define macro1() { /* some stuff */ }
#define macro2() do { /* some stuff */ } while (0)

if (foo) macro1(); else bar;
if (foo) macro2(); else bar;

The second one is correct, but the first one is incorrect (the extra
semi-colon unlinks the `else' from the `if').

Besides, a fake loop, within or outside a macro, can be useful: you jump
out of it with a `break' statement; you could do it with a `goto' but
with the `break' you do not have to bother about managing label names.

--Thomas Pornin
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/