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


Groups > comp.lang.c > #160095

Re: Which side of bitwise OR is evaluated first?

From scott@slp53.sl.home (Scott Lurndal)
Subject Re: Which side of bitwise OR is evaluated first?
Newsgroups comp.lang.c
References <a21102b2-7c82-40d9-9549-c10a1b77247en@googlegroups.com>
Message-ID <O2DdI.1260$RC2.686@fx27.iad> (permalink)
Organization UsenetServer - www.usenetserver.com
Date 2021-04-14 14:33 +0000

Show all headers | View raw


=?UTF-8?B?T8SfdXo=?= <oguzismailuysal@gmail.com> writes:
>I wrote this:
>
>    fp =3D 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 retur=
>ns -1.
>
>The reason why I wrote it this way is that I wanted to handle all errors an=
>d close the file pointer in a single block. But I'm not sure if `ferror' is=
> guaranteed to be called before `fclose' there, and the standard says once =
>`fclose' is called on `fp', what `ferror(fp)' will do is unspecified or som=
>ething like that.
>
>So, should I change this or is it good (except being a bit unreadable)?

 "||" is _not_ a bitwise operator.  It's called 'conditional-or', and
the semantics include not executing the right-most part if the leftmost
part is true.

It's more common to write:

   if (fp && readfrom...)

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


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