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


Groups > comp.lang.c > #8255

Re: #define with semicolon

From Dr Nick <3-nospam@temporary-address.org.uk>
Newsgroups comp.lang.c
Subject Re: #define with semicolon
Date 2011-07-13 21:09 +0100
Message-ID <87mxgivt7j.fsf@temporary-address.org.uk> (permalink)
References <ab0d55fe-25e5-482b-811e-c475aa6065c3@c29g2000yqd.googlegroups.com> <lnpqleq8dh.fsf@nuthaus.mib.org> <2d0b4418-407a-4a9e-9d67-f91fd69b8057@r9g2000yql.googlegroups.com>

Show all headers | View raw


cc <scatnubbs@hotmail.com> writes:

> On Jul 13, 3:38 pm, Keith Thompson <ks...@mib.org> wrote:
>> cc <scatnu...@hotmail.com> writes:
>> > Is it acceptable practice to have a #define with a semicolon in it,
>> > such as:
>>
>> > #define SMALL 1;
>>
>> > I didn't think it was, but a very good friend of mine claims it's
>> > perfectly acceptable if you want to prevent the #define from being
>> > used in an expression like if(SMALL).
>>
>> Why would you want to prevent it from being used in an expression?
>> I think "1;" is a poor example of what your friend is talking about.
>> I'd be interested in seeing a better example.
>
> That was his example. That was also his explanation of why he did it
> (so the compiler would complain if he used it as an expression).
>
> Another example was from the linux kernel.
>
> /usr/src/linux-3.0.0-rc7-mainline/include/linux/mfd/tps65910.h:
> #define LDO_MAX_VOLT 3300;

Flippin' heck. I hope I'm nowhere near the keyboard when anything the
kernal controls get close to that.  Mainline or not.

>> A #define can contain any token sequence you like.  The macro name
>> will be expanded to that token sequence every time you use it.
>> If you want that token sequence to include a semicolon, then you
>> should have a semicolon in the definition.
>
> I know what #define does. I was asking about coding standards more or
> less, and if a #define with a semicolon was commonly used and accepted
> practice.
>
>> But most of the time, a macro expansion is used either in an
>> expression context (in which case it *shouldn't* have any semicolons,
>> and it should be protected by parentheses where necessary), or
>> in a statement context (in which case, if it consists of multiple
>> substatements, you need to use the "do { ... } while (0)" trick).
>>
>
> Right. So you see no logical reason to ever use something like #define
> SMALL 1;? I don't either, but I was just making sure there wasn't
> something I missed.

I can't think of one.  

I had a quick look through my source collection and the only example I
could find where I had a #define ending with a ; was one of those things
where you define a macro one way then include a file, then define it
another and include the file again.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

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


Thread

#define with semicolon cc <scatnubbs@hotmail.com> - 2011-07-13 11:19 -0700
  Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-13 12:38 -0700
    Re: #define with semicolon cc <scatnubbs@hotmail.com> - 2011-07-13 12:56 -0700
      Re: #define with semicolon Dr Nick <3-nospam@temporary-address.org.uk> - 2011-07-13 21:09 +0100
      Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-13 13:30 -0700
        Re: #define with semicolon Hallvard B Furuseth <h.b.furuseth@usit.uio.no> - 2011-07-14 13:03 +0200
        Re: #define with semicolon Gene <gene.ressler@gmail.com> - 2011-07-14 05:27 -0700
        Re: #define with semicolon Roberto Waltman <usenet@rwaltman.com> - 2011-07-15 10:54 -0400
          Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-15 09:05 -0700
            Re: #define with semicolon Robert Wessel <robertwessel2@yahoo.com> - 2011-07-15 11:49 -0500
        Re: #define with semicolon Noob <root@127.0.0.1> - 2011-07-21 15:31 +0200
          Re: #define with semicolon "J. J. Farrell" <jjf@bcs.org.uk> - 2011-07-22 01:51 +0100
      Re: #define with semicolon Harald van Dijk <truedfx@gmail.com> - 2011-07-13 13:51 -0700
        Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-13 14:01 -0700
          Re: #define with semicolon Harald van Dijk <truedfx@gmail.com> - 2011-07-13 14:14 -0700
      Re: #define with semicolon Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-13 17:10 -0600
        Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-13 16:20 -0700
        Re: #define with semicolon Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-14 14:03 +0300
      Re: #define with semicolon Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-14 01:22 +0100
      Re: #define with semicolon Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-22 15:50 +0300
        Re: #define with semicolon Todd Carnes <toddcarnes@gmail.com> - 2011-07-22 21:46 +0000
          Re: #define with semicolon Ian Collins <ian-news@hotmail.com> - 2011-07-23 09:54 +1200
            Re: #define with semicolon Todd Carnes <toddcarnes@gmail.com> - 2011-07-22 22:21 +0000
              Re: #define with semicolon Seebs <usenet-nospam@seebs.net> - 2011-07-23 00:12 +0000
                Re: #define with semicolon Todd Carnes <toddcarnes@gmail.com> - 2011-07-23 00:37 +0000
                Re: #define with semicolon Willem <willem@toad.stack.nl> - 2011-07-23 09:55 +0000
          Re: #define with semicolon Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-22 15:54 -0600
          Re: #define with semicolon Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-23 01:14 +0300
            Re: #define with semicolon Todd Carnes <toddcarnes@gmail.com> - 2011-07-22 22:28 +0000
              Re: #define with semicolon Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-23 02:52 +0300
                Re: #define with semicolon Todd Carnes <toddcarnes@gmail.com> - 2011-07-23 00:14 +0000
          Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-22 16:52 -0700
            Re: #define with semicolon Todd Carnes <toddcarnes@gmail.com> - 2011-07-23 00:07 +0000
  Re: #define with semicolon Ben Pfaff <blp@cs.stanford.edu> - 2011-07-13 13:04 -0700
  Re: #define with semicolon Barry Schwarz <schwarzb@dqel.com> - 2011-07-13 17:11 -0700
  Re: #define with semicolon Walter Banks <walter@bytecraft.com> - 2011-07-14 08:11 -0400
  Re: #define with semicolon pete <pfiland@mindspring.com> - 2011-07-14 08:24 -0400
  Re: #define with semicolon "io_x" <a@b.c.invalid> - 2011-07-14 17:34 +0200
  Re: #define with semicolon cc <scatnubbs@hotmail.com> - 2011-07-15 04:12 -0700
    Re: #define with semicolon Anders Wegge Keller <wegge@wegge.dk> - 2011-07-15 14:49 +0200
    Re: #define with semicolon Snit <usenet@gallopinginsanity.com> - 2011-07-15 08:37 -0700
      Re: #define with semicolon "cc" <scatnubs@hotmail.com> - 2011-07-19 11:03 -0700
    Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-15 09:23 -0700
      Re: #define with semicolon cc <scatnubbs@hotmail.com> - 2011-07-15 10:37 -0700
        Re: #define with semicolon Snit <usenet@gallopinginsanity.com> - 2011-07-15 13:07 -0700
          Re: #define with semicolon Keith Thompson <kst-u@mib.org> - 2011-07-15 13:26 -0700
            Re: #define with semicolon Snit <usenet@gallopinginsanity.com> - 2011-07-15 13:31 -0700
              Re: #define with semicolon cc <scatnubbs@hotmail.com> - 2011-07-19 08:55 -0700
                Re: #define with semicolon "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-07-19 14:12 -0700
              Re: #define with semicolon "cc" <scatnubs@hotmail.com> - 2011-07-19 11:05 -0700
                Re: #define with semicolon cc <scatnubbs@hotmail.com> - 2011-07-19 11:18 -0700
                Re: #define with semicolon Snit <usenet@gallopinginsanity.com> - 2011-07-19 11:23 -0700
                Re: #define with semicolon cc <scatnubbs@hotmail.com> - 2011-07-19 11:31 -0700
          Re: #define with semicolon "cc" <scatnubs@hotmail.com> - 2011-07-19 11:05 -0700
      Re: #define with semicolon Snit <usenet@gallopinginsanity.com> - 2011-07-15 10:51 -0700
        Re: #define with semicolon "cc" <scatnubs@hotmail.com> - 2011-07-19 11:04 -0700
          Re: #define with semicolon "J. J. Farrell" <jjf@bcs.org.uk> - 2011-07-20 00:53 +0100
            Re: #define with semicolon Snit <usenet@gallopinginsanity.com> - 2011-07-19 16:57 -0700
  Re: #define with semicolon gordonb.ethca@burditt.org (Gordon Burditt) - 2011-07-15 10:33 -0500
  Re: #define with semicolon Michael Angelo Ravera <maravera@prodigy.net> - 2011-07-22 10:55 -0700

csiph-web