how to make a macro work as a single line if stmt without braces

Liste des GroupesRevenir à cl c  
Sujet : how to make a macro work as a single line if stmt without braces
De : mark (at) *nospam* qtrac.eu (Mark Summerfield)
Groupes : comp.lang.c
Date : 21. Sep 2024, 09:35:49
Autres entêtes
Message-ID : <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk>
User-Agent : Pan/0.149 (Bellevue; 4c157ba)
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'

Date Sujet#  Auteur
21 Sep09:35 * how to make a macro work as a single line if stmt without braces6Mark Summerfield
21 Sep10:10 +- Re: how to make a macro work as a single line if stmt without braces1Lawrence D'Oliveiro
21 Sep10:47 +- Re: how to make a macro work as a single line if stmt without braces1David Brown
21 Sep11:13 +* Re: how to make a macro work as a single line if stmt without braces2Bart
21 Sep11:53 i`- Re: how to make a macro work as a single line if stmt without braces1Mark Summerfield
21 Sep11:15 `- Re: how to make a macro work as a single line if stmt without braces1Ike Naar

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal