Phillip Pearson - Second p0st

tech notes and web hackery from the guy that brought you bzero, python community server, the blogging ecosystem, the new zealand coffee review and the internet topic exchange

2005-2-23

C macro gotcha

Argh - I fell for the obvious C macro gotcha. I had a macro like this:

#define FOO(x) do_something(); do_something_else()

but I was calling it like this:

if (some_condition) FOO(x);

This is bad! It is equivalent to this:

if (some_condition) do_something(); do_something_else();

So do_something_else() always gets called. Oops!

Normally I write my macros in the recommended way:

#define FOO(x) do { do_something(); do_something_else(); } while (0)

Not feeling particularly smart today. Hmm.

... more like this: []