Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #160100
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Which side of bitwise OR is evaluated first? |
| Date | 2021-04-14 10:26 -0700 |
| Organization | None to speak of |
| Message-ID | <87a6q03hgi.fsf@nosuchdomain.example.com> (permalink) |
| References | <a21102b2-7c82-40d9-9549-c10a1b77247en@googlegroups.com> <s566dc$knc$1@dont-email.me> |
David Brown <david.brown@hesbynett.no> writes:
> (Please try to get a proper newsreader and newsserver, rather than
> google groups - gg is particularly inconvenient when you have code
> snippets, making it hard to fix gg's broken formatting.)
>
> On 14/04/2021 07:43, Oğuz wrote:
>> I wrote this:
>>
>> fp = fopen(optarg, "r");
>> if (!fp || (readfrom(fp), ferror(fp)|fclose(fp)))
>> perror(optarg)
>>
>> `readfrom' is a function of type `void' that calls `getline' until
>> it returns -1.
>>
>
> C guarantees that the left side of || and && is evaluated (including
> side-effects) first, and the right side is then evaluated only if
> necessary (depending on the first side). So the check of !fp first is
> safe. Similarly, the comma operator evaluates the left side first, then
> the right side.
>
> Other operators, such as | and &, have no such rules. The evaluation of
> the left and right sides are unsequenced, meaning not only that their
> order of evaluation is not fixed, but the can be interleaved and it can
> vary between runs of the code. Thus the "ferror(fp)" and "fclose(fp)"
> can be done in any order - and the code can in theory do half of
> fclose() first, then ferror(), then the rest of fclose().
Function calls cannot be interleaved like that. However, standard
library calls can be implemented as macros. And in any case calling
ferror(fp) after close(fp) has undefined behavior.
N1570 6.5.2.2p10:
There is a sequence point after the evaluations of the function
designator and the actual arguments but before the actual
call. Every evaluation in the calling function (including other
function calls) that is not otherwise specifically sequenced
before or after the execution of the body of the called function
is indeterminately sequenced with respect to the execution of
the called function.
and a footnote:
In other words, function executions do not ‘‘interleave’’ with each other.
[...]
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Which side of bitwise OR is evaluated first? Oğuz <oguzismailuysal@gmail.com> - 2021-04-13 22:43 -0700
Re: Which side of bitwise OR is evaluated first? Barry Schwarz <schwarzb@delq.com> - 2021-04-14 00:04 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 09:42 +0200
Re: Which side of bitwise OR is evaluated first? Oğuz <oguzismailuysal@gmail.com> - 2021-04-14 00:51 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 11:25 +0200
Re: Which side of bitwise OR is evaluated first? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-04-14 03:27 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 13:28 +0200
Re: Which side of bitwise OR is evaluated first? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-14 10:31 -0700
Re: Which side of bitwise OR is evaluated first? David Brown <david.brown@hesbynett.no> - 2021-04-14 19:40 +0200
Re: Which side of bitwise OR is evaluated first? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-14 10:26 -0700
Re: Which side of bitwise OR is evaluated first? Richard Damon <Richard@Damon-Family.org> - 2021-04-14 07:22 -0400
Re: Which side of bitwise OR is evaluated first? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-04-14 07:54 -0700
Re: Which side of bitwise OR is evaluated first? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-14 14:23 -0400
Re: Which side of bitwise OR is evaluated first? scott@slp53.sl.home (Scott Lurndal) - 2021-04-14 14:33 +0000
Re: Which side of bitwise OR is evaluated first? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-14 13:15 -0400
Re: Which side of bitwise OR is evaluated first? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-04-14 08:06 -0700
Re: Which side of bitwise OR is evaluated first? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-04-14 10:34 -0600
Re: Which side of bitwise OR is evaluated first? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-11 00:24 -0700
csiph-web