mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Pornin <Thomas.Pornin@ens.fr>
To: ailinykh@usa.net
Cc: linux-kernel@vger.kernel.org
Subject: Re: fake loop
Date: Fri, 3 Aug 2001 18:07:49 +0200	[thread overview]
Message-ID: <20010803180749.A42133@bolet.ens.fr> (raw)
In-Reply-To: <20010803155735.18620.qmail@nwcst31f.netaddress.usa.net>

In article <20010803155735.18620.qmail@nwcst31f.netaddress.usa.net> you write:
> #define prepare_to_switch()     do { } while(0)

This one is a classic C trick; it is documented in the K&R book. With
this fake loop, the macro can be used like a statement anywhere. For
instance, compare the two following :

#define macro1()	{ /* some stuff */ }
#define macro2()	do { /* some stuff */ } while (0)

if (foo) macro1(); else bar;
if (foo) macro2(); else bar;

The second one is correct, but the first one is incorrect (the extra
semi-colon unlinks the `else' from the `if').


Besides, a fake loop, within or outside a macro, can be useful: you jump
out of it with a `break' statement; you could do it with a `goto' but
with the `break' you do not have to bother about managing label names.


	--Thomas Pornin

  reply	other threads:[~2001-08-03 16:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-03 15:57 Andrey Ilinykh
2001-08-03 16:07 ` Thomas Pornin [this message]
2001-08-03 16:11 ` Martin Mačok
2001-08-03 16:13 ` Crutcher Dunnavant
2001-08-03 16:25 ` Mike Frisch
2001-08-03 16:44 ` Richard B. Johnson
2001-08-04  0:13   ` J . A . Magallon
2001-08-04 18:47     ` Thomas Pornin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010803180749.A42133@bolet.ens.fr \
    --to=thomas.pornin@ens.fr \
    --cc=ailinykh@usa.net \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®