Path: csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch 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 06:07:15 -0700 Organization: A noiseless patient Spider Lines: 38 Message-ID: <86o74h88ak.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sat, 21 Sep 2024 15:07:16 +0200 (CEST) Injection-Info: dont-email.me; posting-host="97ef20b65758fbd99b7a73cba003feee"; logging-data="1693884"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BVd1dr55DBNy/KM3Z3nvgW89s6DgJO1c=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:ngzyyI5dj09UxVUfoMO7GleKi48= sha1:cqr0RMOVruy2Uv9PzP4t8QGbQQk= Xref: csiph.com comp.lang.c:388461 Mark Summerfield 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.