Sujet : Re: how to make a macro work as a single line if stmt without braces
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 21. Sep 2024, 23:55:12
Autres entêtes
Organisation : None to speak of
Message-ID : <87bk0gr50v.fsf@nosuchdomain.example.com>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13)
Tim Rentsch <
tr.17687@z991.linuxsc.com> writes:
Mark Summerfield <mark@qtrac.eu> writes:
I have this macro:
>
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
>
which I use like this:
>
total++;
if (failed) {
WARN("failed because...");
} else
ok++;
>
I would prefer to be able to write this instead:
>
total++;
if (failed)
WARN("failed because...");
else
ok++;
>
but doing so results in a compiler error:
>
error: 'else' without a previous 'if'
>
You can define WARN() this way
>
#define WARN(...) ( \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__), \
fprintf(stderr, __VA_ARGS__) \
)
>
and it will work as you want it to.
Good point. To expand on that, the do/while trick (without the trailing
semicolon) is the usual way to write a macro that can be used anywhere a
statement can appear. But it's not necessary if all you want is a
sequence of expression statements. In that case, you can write the
expression statements (terminated by semicolons) as expressions
(separated by comma operators and enclosed in parentheses), and the
whole expansion is a single expression that can be used in an expression
statement. The macro can then be used in more contexts, since its
expansion is an expression (though that's not likely to matter in this
case).
An if/else can be turned into a conditional operator, which also gives
you an expression, but there's no expression equivalent for most control
flow constructs (loops, return, etc.). That's where the do/while trick
becomes necessary.
For this example (two fprintf calls), you can use either do/while or a
comma operator. You might consider using the do/while in case you might
want to add control flow later. If you have several similar macros,
there's something to be said for being consistent.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */