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


Groups > comp.lang.c > #162033

Re: How to disambiguate macro?

From John Forkosh <forkosh@panix.com>
Newsgroups comp.lang.c
Subject Re: How to disambiguate macro?
Date 2021-07-22 05:30 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <sdavpm$nkl$1@reader1.panix.com> (permalink)
References (3 earlier) <87fsw9m4qd.fsf@bsb.me.uk> <sd7ud2$hkh$1@reader1.panix.com> <sd8r8p$fph$1@dont-email.me> <sd8rra$f4o$1@reader1.panix.com> <sda3ah$l3i$1@dont-email.me>

Show all headers | View raw


James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
> John Forkosh wrote:
>> James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
>>> John Forkosh wrote:
>>> ...
>>>> i.e., "What the heck is he doing that for?" But then again, I never
>>>> much liked relying that  (a&&b)  always evaluates a first, immediately
>>>> becoming 0 without evaluating b at all if a is itself 0. You never know
>>>> exactly what the next C standard might mess around with.
>>>
>>> That is a pretty fundamental feature of C; it's one of the last features
>>> of C that I would ever expect to see changed. It would break far too
>>> much existing code.
>> 
>> I wouldn't be too complacent about that in the future.
> 
> I suspect that reflects a lack of experience with C. If you had a lot of
> experience with C, and in particular, a lot of experience reading other
> people's C code, you should have noticed that code which relies upon
> this feature of the && and || operators is extremely common.

I've been using mostly C (and some Fortran and various assemblers)
since 1984, and before that (first paying job in 1966) Fortran,
various assemblers, PL/1, Cobol, Basic and some other languages
you never heard of like Jovial. And as an independent contractor
since 1986 with contracts between 3months and 2.5years, I've read
tons (maybe megatons) of other people's C code. But can't explicitly
recall ever seeing && used as a guard. While I'm sure it must've been
there in that way somewheres/sometimes, I'm pretty sure I'd have
remembered if that were common practice. And while I've been aware
of that guard-like && semantics since K&R1 (pg19), I don't recall
seeing them ever suggest or illustrate using it as a guard.
That's just (as mentioned above) "weird" -- i.e., whereas anybody
can guess what if(a)b; means, the syntax a&&b doesn't by itself
>>connote<< any guard-like semantics. I'd use it that way if some
goofy syntactic situation warranted it -- like maybe Ben's answer
as a workaround for my original question, but never when if(a)b;
would work just as will, which is pretty much all the time.
Or maybe not: let's see a few snippets besides Ben's where you
feel a&&b as a guard is the better solution...

> The C committee is reluctant to make any change that would break
> a significant amount of existing strictly conforming code - they would
> certainly not approve a change that would break as much existing code
> as this one would.
> 
>> And even today I wouldn't be so blithely sure.
>> For example, && is commutative, i.e., a&&b == b&&a.
> 
> As others have pointed out, that's explicitly not the case in C,

Yeah, I meant (and thought it was clear) commutative with respect to
the logical result, not with respect to the guard-like side effects.

> and there's lots of code that relies upon that fact in order to
> achieve it's desired effects.

Yeah, I see I was wrong suggesting some future standard (or present
implementation) might behave differently. I still don't like it,
but it's probably safe to assume it'll work as intended for the
forseeable future.

>> So suppose you're compiling with -O3 optimization,
>> and your program contains something like...
>>    ( (very_very_complicated_expression) && (++n>3) )
>> So maybe the optimizer says to itself, "Hey, I'll
>> just execute that very simple right-hand-side first,
>> and if it's false then I don't have to bother evaluating
>> that very complicated left-hand-side at all. Could save lots
>> of time." Now maybe that's not supposed to happen,
>> but it's way too subtle for my liking.
> 
> No conforming compiler is allowed to do that, and any compiler which did
> would quickly break large quantities of existing code. Developers would
> abandon any such compiler quite quickly - and that's a more important
> issue than the fact that it doesn't conform to the standard.
> 
>> I'd never consider a&&b to mean that a is a guard on b's execution.
> 
> Which implies that you're not very experienced as a C programmer. Only a
> newbie would have any uncertainty on that matter.
> 
>> It certainly doesn't look like a guard, and I wouldn't
>> trust interpreting it that way. Clearly very bad practice
>> and very bad advice, even if it happens to work.
>
> It's guaranteed to work by the standard, deliberately with the intent of
> allowing developers to rely upon that fact, and most C programmers have
> written code that relies upon that fact for it's successful execution.
> Incidentally, the same is true of C++ - and a gratuitous incompatibly
> between C and C++ goes against the official policies of both the C
> committee and the C++ committee.

-- 
John Forkosh  ( mailto:  j@f.com  where j=john and f=forkosh )

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


Thread

How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 11:15 +0000
  Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-20 13:49 +0100
    Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 12:56 +0000
      Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-20 15:36 +0100
        Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-21 01:48 +0000
          Re: How to disambiguate macro? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-20 19:18 -0700
          Re: How to disambiguate macro? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-20 20:27 -0700
            Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-21 04:15 +0000
              Re: How to disambiguate macro? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-21 02:00 -0700
              Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-21 06:02 -0400
          Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-21 06:00 -0400
            Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-21 10:10 +0000
              Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 11:30 +0100
              Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 13:31 +0200
              Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 13:02 +0100
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 14:20 +0100
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 15:15 +0100
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 16:28 +0200
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 16:20 +0100
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 17:34 +0100
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 19:46 +0200
                Re: How to disambiguate macro? Manfred <noname@add.invalid> - 2021-07-21 20:42 +0200
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-22 08:37 +0200
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 20:47 +0100
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-22 08:47 +0200
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-22 11:31 +0100
                Re: How to disambiguate macro? antispam@math.uni.wroc.pl - 2021-07-22 14:37 +0000
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-22 18:15 +0200
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 21:19 +0100
                Re: How to disambiguate macro? Manfred <noname@add.invalid> - 2021-07-21 15:42 +0200
                Re: How to disambiguate macro? scott@slp53.sl.home (Scott Lurndal) - 2021-07-21 15:39 +0000
              Re: How to disambiguate macro? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-21 09:22 -0700
              Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-21 17:24 -0400
                Re: How to disambiguate macro? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-21 16:42 -0700
                Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-22 05:49 +0000
                Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-22 19:11 -0400
                Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-22 05:30 +0000
                Re: How to disambiguate macro? scott@slp53.sl.home (Scott Lurndal) - 2021-07-22 14:14 +0000
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-22 17:06 +0100
                OT: possessive adjectives (Was: How to disambiguate macro?) Manfred <noname@add.invalid> - 2021-07-22 15:04 +0200
                Re: OT: possessive adjectives Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-08-14 13:37 -0700
          Re: How to disambiguate macro? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-08-14 13:58 -0700
  Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-20 14:50 +0200
    Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 13:19 +0000
      Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-20 14:40 +0100
        Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-20 21:02 +0200
      Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-20 21:00 +0200
      Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 08:15 +0200
      Re: How to disambiguate macro? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-08-14 23:17 -0700
  Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 12:51 +0000
  Re: How to disambiguate macro? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-20 08:47 -0700

csiph-web