Deutsch English Français Italiano |
<vcm16e$1hm2u$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> 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:47:10 +0200 Organization: A noiseless patient Spider Lines: 77 Message-ID: <vcm16e$1hm2u$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 10:47:11 +0200 (CEST) Injection-Info: dont-email.me; posting-host="873da720fc09e02ec6d3f7f1ca590b8d"; logging-data="1628254"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18otCM1MyEM8t4Lf9GZ8EjBL7hmC7xmBmM=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:DA31WiWLXL1pCAMvlfRCRtH40HI= In-Reply-To: <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk> Content-Language: en-GB Bytes: 3221 On 21/09/2024 09: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' You should get in the habit of being consistent with braces, and being generous with them. Your future self with thank you. if (failed) { WARN("failed because..."); } else { ok++; } The rule here is that you the /only/ time you allow yourself to omit the braces is if you have a very simple "if" that has no "else", is small enough to fit entirely on a single line, and the statement could not possibly be misinterpreted, use a macro, or have any other non-obvious behaviour. Thus : if (failed) return -1; // Early exit or if (!failed) ok++; Otherwise, put in all the braces. This also keeps your indentation consistent - open braces are only at the end of a line, and the next line starts an indented block. Close braces are only ever the first non-indent character in a line, and you un-indent on the close brace. (It's common to put the opening brace of a function at the start of its own line, and braces for struct or array initialisation can be freer.) do, while and for loops always use braces. There are a few other variant styles that are also consistent and clear, but usually they end up unnecessarily spread out. (Opinions will vary about that.) But getting good habits here from early one will make your code much clearer, greatly reduce the risk of blocking errors, and be a significant boon for code maintenance and the use of version control systems.