🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Macros and Braces

#2367
From: S. Lee
Date: Sun, 24 Mar 1996 17:40
44 lines
1420 bytes
In article <4j06iu$7qm@solutions.solon.com>,
Antony Pace (UK/CH) <100576.1776@compuserve.com> wrote:
>Hi all,
>
>I am having problems writing a macro (Ansi C) for which I feel there
>must be standard technique.  Can anyone help?
>
>#define CLEAR_ERROR(e) { \
>  (e).a = 0;             \
>  (e).b = 0;             \
>  (e).c = 0;             \
>  TRACE("clear error");  \
>}

You can use

do {
  (e).a = 0;
  ...
} while (0)

for this macro.

>TRACE is a macro provided by another module which is approximately:
>
>#define TRACE(s)                                    \
>  if (trace_on)                                     \
>    fprintf(trace_file, __FILE__ __LINE__ "%s", s);

if (!trace_on) {
} else
  fprintf(trace_file, ...)

or

do {
  if (trace_on)
    fprintf(trace_file, __FILE__ __LINE__ "%s", s);
} while (0)

This will let you use the macros in a if statement without fear of "else"
getting the wrong "if" (or not getting one at all).

Stephen
Technical Details
Message-ID: <4j41er$nfo@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: sl14@crux1.cit.cornell.edu (S. Lee)
Newsgroups: comp.lang.c.moderated
Subject: Re: Macros and Braces
Date: 24 Mar 1996 11:40:43 -0600
Organization: Nekomi Institute of Technology
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4j41er$nfo@solutions.solon.com>
References: <4j06iu$7qm@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com