🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2394

Re: Macros and Braces

#2394
From: Richard Krehbiel
Date: Wed, 27 Mar 1996 00:59
35 lines
1666 bytes
100576.1776@compuserve.com (Antony Pace (UK/CH)) wrote:

[...much deleted...]

>The comma expression
>--------------------

>#define CLEAR_ERROR(e)  \
>  (e).a = 0, (e).b = 0 ,  (e).c = 0 , TRACE("clear error")

>which expands to 

>  (e).a = 0, (e).b = 0 ,  (e).c = 0 ,  if (trace_on)
>    fprintf(trace_file, __FILE__ __LINE__ "%s", "clear error")
>  ;

>The semicolon is now a statement separator and not a null statement, but
>the TRACE expands to an if statement which does not work with the comma
>expression.

If you can rewrite the TRACE macro, you can change it from a
"statement" to an "expression" by using the short-circuiting
properties of the && operator as follows:

#define TRACE(msg) (void) (trace_on && \
   fprintf(trace_file, "%s[%d]: %s", __FILE__, __LINE__, msg))

....and then your comma-expression version of CLEAR_ERROR will work.
In fact, this also makes your comma-expression version into an
"expression", rather than a "statement" as it would be using the
"while" trick, which may (or may not) be desirable.

--
Richard Krehbiel, Kastle Systems, Arlington VA USA
rich@kastle.com (work) or richk@mnsinc.com (personal)
Technical Details
Message-ID: <4ja3td$pco@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4j06iu$7qm@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: rich@kastle.com (Richard Krehbiel)
Newsgroups: comp.lang.c.moderated
Subject: Re: Macros and Braces
Date: 26 Mar 1996 18:59:25 -0600
Organization: Kastle Development Associates
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4ja3td$pco@solutions.solon.com>
References: <4j06iu$7qm@solutions.solon.com>
Reply-To: rich@kastle.com
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Forte Free Agent 1.0.82