Deutsch   English   Français   Italiano  
<slrnvet3lr.8b8.ike@iceland.freeshell.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Ike Naar <ike@sdf.org>
Newsgroups: comp.lang.c
Subject: Re: how to make a macro work as a single line if stmt without braces
Date: Sat, 21 Sep 2024 09:15:40 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <slrnvet3lr.8b8.ike@iceland.freeshell.org>
References: <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk>
Injection-Date: Sat, 21 Sep 2024 11:15:40 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="546acf6d755ca4358d09f1c5386b815c";
	logging-data="1636204"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/GcldvGGEgvAPgMtGsSxmC"
User-Agent: slrn/1.0.3 (Patched for libcanlock3) (NetBSD)
Cancel-Lock: sha1:Y4eD32DKvQqPfvuQoOirmqYUA48=
Bytes: 1779

On 2024-09-21, Mark Summerfield <mark@qtrac.eu> wrote:
> I have this macro:
>
> #define WARN(...)                                       \
>     do {                                                \
>         fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
>         fprintf(stderr, __VA_ARGS__);                   \
>     } while (0);
>
> [...]
>
> 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'

After expansion of the WARN macro there are two semicolons before the 'else'.
The first semicolon is from the

    } while (0);

line, the second is from the

    
    WARN("failed because...");

line. 
Removing the semicolon from one of these two lines fixes the compiler error.