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


Groups > comp.lang.c > #11332

behavior of || operator

From "Mark" <mark_cruzNOTFORSPAM@hotmail.com>
Newsgroups comp.lang.c
Subject behavior of || operator
Date 2011-10-07 13:13 -0400
Organization Aioe.org NNTP Server
Message-ID <j6nc0d$dii$1@speranza.aioe.org> (permalink)

Show all headers | View raw


Hello,

#define ATTACH           1
#define DETACH           2

#define DIRECTION_INGRESS       1   /* Incoming */
#define DIRECTION_EGRESS        2   /* Outgoing */



if ((action != ATTACH) || (action != DETACH))

    puts("invalid action");

if ((dir != DIRECTION_INGRESS) || (dir != DIRECTION_EGRESS))
    puts("invalid direction");



I appears that condition within if() is verified for being TRUE or FALSE, 
and it is sufficient to have just one operand of || operator to return TRUE 
in order for the whole expression yield TRUE result.



For example, if action = 1, then (action != ATTACH) becomes FALSE, then 
(action != DETACH) is evaluated and it will return TRUE, thus making the 
statement within if() TRUE. And this is wrong behavior, given that I need to 
have actoni be either 1 or 2.



To make it right, || must be replaced with &&.



Is my reasoing above correct and valid?



Thanks.



Mark.

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


Thread

behavior of || operator "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2011-10-07 13:13 -0400
  Re: behavior of || operator James Kuyper <jameskuyper@verizon.net> - 2011-10-07 14:11 -0400
  Re: behavior of || operator John Gordon <gordon@panix.com> - 2011-10-07 18:47 +0000
  Re: behavior of || operator Willem <willem@toad.stack.nl> - 2011-10-07 19:50 +0000
  Re: behavior of || operator Gene <gene.ressler@gmail.com> - 2011-10-08 13:58 -0700

csiph-web