Harald van D?k
7/13/2011 9:14:00 PM
On Jul 13, 10:01 pm, Keith Thompson <ks...@mib.org> wrote:
> Harald van D?k <true...@gmail.com> writes:
> > On Jul 13, 8:56 pm, cc <scatnu...@hotmail.com> wrote:
> >> Right. So you see no logical reason to ever use something like #define
> >> SMALL 1;? I don't either, but I was just making sure there wasn't
> >> something I missed.
>
> > I do, though it does not apply to your case. There are lint-like tools
> > that allow you to declare that a macro expands to a statement. The
> > tool will verify that it is only ever used as a statement, but in
> > return, it has to actually *be* a statement, or it gets very confused.
> > A macro expansion that would be a statement if you add a semicolon
> > does not qualify.
>
> From your description, it sounds like there are some bad lint-like
> tools out there.
>
> A macro that's intended to expand to a statement (and not to an
> expression) should use the "do { ... } while (0)" trick to avoid
> problems when used with if/else.
That depends. As long as it warns for empty statements, which includes
the cases where the macro is immediately followed by a semicolon, it
is fine. Regardless of whether the macro appears in an if statement,
it expects the macro to always be used by itself. And if it is always
used by itself, it causes no problems before an else: it looks just as
you would normally use it. It is just as valid as far as C is
concerned. The main thing it has going against it is that it gets very
confusing when you mix it with macros that do expect to be followed by
a semicolon. (I don't use it myself, by the way.)