Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15851

Re: macro

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: macro
Date 2012-01-16 12:01 -0800
Organization None to speak of
Message-ID <lnhazv1kon.fsf@nuthaus.mib.org> (permalink)
References <89018f82-9f45-4e72-a0a2-8424e91752b6@g41g2000yqa.googlegroups.com> <87boq5ozau.fsf@temporary-address.org.uk> <8645ca39-0a48-4efb-af29-86f84eb04cd5@h3g2000yqe.googlegroups.com> <92fa9a4a-692b-4ffd-a733-97a0b417a669@f1g2000yqi.googlegroups.com> <jf1dcn$ign$1@reader1.panix.com>

Show all headers | View raw


jgk@panix.com (Joe keane) writes:
> In article <92fa9a4a-692b-4ffd-a733-97a0b417a669@f1g2000yqi.googlegroups.com>,
> christian.bau <christian.bau@cbau.wanadoo.co.uk> wrote:
>>You probably want to use the well-known trick do create a macro that
>>is a single statement without semicolon like:
>>
>>#define LOG (level, msg) do { whatever (); } while (0)
>>
>>and get rid of that rubbish where you use a ternary operator to save
>>an if statement, which is just rubbish and demonstrates that someone
>>tries to look clever without actually being clever.
>
> Silly.
>
> Of *course* you want a macro to be expression if that's easily done.
>
> e.g.
>
> 	#define JK_GET_Z(JK, Z) \
> 	(((JK)->a & 0x1) == 0 ? \
> 	((Z) = (JK)->b >> 2 & 0x1, 0) : JK_GET_XZ(JK, Z))
>
> You could write
>
> 	#define JK_GET_Z(JK, Z) \
> 	do { \
> 	  if (((JK->a) & 0x1) == 0) \
> 	    (Z) = (JK)->b >> 2 & 0x1; \
> 	  else
> 	    JK_GET_XZ(JK, Z); \
> 	} while (0)
>
> but *why*?

If you were writing this other than as a macro, presumably you'd use the
if/else from rather than using the conditional operator, simply because
it's more readable that way.  (Well, I almost certainly would.)

Yes, if you write the macro as an expression, you can use it in more
contexts.  But what if you happen to know that you're *never* going to
need to use it other than in a statement context?  Why not write the
macro definition in a more readable and maintainable form, when the only
cost is the loss of some flexibility that you're never going to use
anyway?  Unless it's a visible part of a general-purpose library, if you
find later that it needs to be an expression you can always go back and
change it.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:29 -0800
  Re: macro Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-15 13:44 +0000
    Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:58 -0800
      Re: macro "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> - 2012-01-15 15:34 +0100
      Re: macro "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-01-15 06:37 -0800
        Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 07:28 -0800
          Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:30 -0800
            Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 00:18 -0800
              Re: macro Hans Vlems <hvlems@freenet.de> - 2012-01-16 00:52 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 22:08 +1300
                Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 03:04 -0800
                Re: macro Markus Wichmann <nullplan@gmx.net> - 2012-01-16 12:42 +0100
        Re: macro jgk@panix.com (Joe keane) - 2012-01-16 14:48 +0000
          Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-16 12:01 -0800
            Re: macro Philip Lantz <prl@canterey.us> - 2012-01-17 01:02 -0800
              Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-17 13:05 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 13:12 +1300
                Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 01:05 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 22:57 +1300
                Re: macro "osmium" <r124c4u102@comcast.net> - 2012-01-18 06:22 -0600
                Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 07:50 -0800
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 14:09 +1300
                Re: macro blp@cs.stanford.edu (Ben Pfaff) - 2012-01-18 11:28 -0800
                Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 21:09 +0000
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:08 +1300
                [OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:58 +1300
                Re: [OT] Re: macro Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-20 12:22 +0000
                Re: [OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-21 07:58 +1300
                Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-19 22:47 -0500
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 17:06 +1300
                Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-20 00:12 -0500
                Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 18:28 +1300
                Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-20 03:21 -0800
                Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 10:50 -0500
                Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 11:46 -0500
                Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-18 14:24 -0800
                Re: macro jgk@panix.com (Joe keane) - 2012-01-18 22:36 +0000
                Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 22:50 +0000
            Re: macro jgk@panix.com (Joe keane) - 2012-01-17 22:08 +0000
  Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-15 08:00 -0800
  Re: macro NEON China BLUE <chine.bleu@yahoo.com> - 2012-01-15 08:15 -0800
  Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 09:04 +1300
    Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:36 -0800
  Re: macro Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-16 12:01 +0000

csiph-web