Deutsch English Français Italiano |
<vcm2mr$1humj$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!2.eu.feeder.erje.net!feeder.erje.net!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Bart <bc@freeuk.com> 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 10:13:00 +0100 Organization: A noiseless patient Spider Lines: 38 Message-ID: <vcm2mr$1humj$1@dont-email.me> References: <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 21 Sep 2024 11:13:00 +0200 (CEST) Injection-Info: dont-email.me; posting-host="e721e00107fc1cc5a0da1c4653c9f2db"; logging-data="1637075"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pWJNFKU2RcTDqPn1hJN3Y" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:P9lw6ODGEV1oRPV4XZkv7lBY94g= In-Reply-To: <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk> Content-Language: en-GB Bytes: 2022 On 21/09/2024 08:35, Mark Summerfield wrote: > 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' Remove the semicolon at the end of the macro. As it is, it compiles to: if (failed) do {...} while 0;; else ok++: There are two semicolons together, the second terminates the whole if statement, leaving an open 'else'.