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


Groups > comp.lang.c > #156766

Re: Question About &&

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Question About &&
Date 2020-11-28 02:23 +0000
Organization A noiseless patient Spider
Message-ID <87blfi43ub.fsf@bsb.me.uk> (permalink)
References <c21717e3-823c-48ff-92f8-9818f26c60aan@googlegroups.com>

Show all headers | View raw


"das...@gmail.com" <dashley@gmail.com> writes:

> I have a while() loop like this:
>
> while (
>                  (i < in_buflen)  /* Still chars left, and short-circuit guard for next clause. */
>                  &&
>                  (strchr(allowed_subsequent_chars, in_buf[i]))
>                )
>
> i is size_t, and the result of strchr() is char *.  I'm using the
> second condition as a substitute for comparison to NULL.  The test as
> written makes me nervous.
>
> Here is my question: if I use "&&" with types of very different sizes,
> for example:
>
> char c;
> char *pc;
> if (c && pc) ...
>
> is this functionally equivalent to:
>
> if ((c != '\0') && (pc != NULL)) ...
>
> ???
>
> My gut tells me yes, but I'm not sure.

Your gut is sound.  In fact && is defined in terms of comparing the
operands unequal to zero:

6.5.13 Logical AND operator
Syntax
1   logical-AND-expression:
         inclusive-OR-expression
         logical-AND-expression && inclusive-OR-expression

Constraints
2   Each of the operands shall have scalar type.

Semantics
3   The && operator shall yield 1 if both of its operands compare unequal
    to 0; otherwise, it yields 0. The result has type int.

> Is there any unexpected
> behavior if one omits the "!=" tests and uses data types of very
> different sizes?

The different sizes (or types) don't come into it.  && is not like the
arithmetic operators where the operands are converted to a common type
before the expression is evaluated.  Each operand is treated entirely
separately.

-- 
Ben.

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


Thread

Question About && "das...@gmail.com" <dashley@gmail.com> - 2020-11-27 16:23 -0800
  Re: Question About && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-11-28 02:23 +0000
  Re: Question About && Richard Damon <Richard@Damon-Family.org> - 2020-11-27 21:24 -0500
  Re: Question About && "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-11-27 19:39 -0800
    Re: Question About && gazelle@shell.xmission.com (Kenny McCormack) - 2020-11-28 04:48 +0000
      Re: Question About && "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-11-28 00:03 -0800
        Re: Question About && James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-11-28 12:17 -0500
          Re: Question About && "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-11-28 16:10 -0800
          Re: Question About && Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-04 00:06 -0800
            Re: Question About && "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2020-12-04 06:39 -0800
              Re: Question About && Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-06 16:41 -0800
                Re: Question About && "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2020-12-07 07:20 -0800
      Re: Question About && "das...@gmail.com" <dashley@gmail.com> - 2020-11-28 14:37 -0800
        Re: Question About && Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-11-28 14:51 -0800
        Re: Question About && "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-11-28 16:53 -0800
    Re: Question About && James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-11-28 12:25 -0500
      Re: Question About && "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-11-28 16:13 -0800

csiph-web