Phillip Pearson - web + electronics notes

tech notes and web hackery from a new zealander who was vaguely useful on the web back in 2002 (see: python community server, the blogging ecosystem, the new zealand coffee review, 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: []