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


Groups > comp.lang.c > #23318

Precedence Misunderstanding

From Charles Richmond <frizzle@aquaporin4.com>
Newsgroups comp.lang.c
Subject Precedence Misunderstanding
Date 2012-07-16 23:18 -0500
Organization Canine Computer Center
Message-ID <ju2p36$qj9$1@dont-email.me> (permalink)

Show all headers | View raw


Consider the following program:

#include  <stdio.h>
#include  <stdlib.h>

int main()
{
    int i = 4, j = 12, k = 41;

    printf("expression is %d + %d * %d\n",k,j,i);
    printf("--> value  is %d\n",k + j * i);
}

When compiled and run, the output was:

expression is 41 + 12 * 4
--> value  is 89

Because the multiply operator has a *higher* precedence than the 
addition operator... the multiply is done first. So the result is 
89, and *not* 212.


Now take the following program:

#include  <stdio.h>
#include  <stdlib.h>

int  callthis();

int main()
{
    int i = 1, j = 1, k = 0;

    printf("expression is %s\n","i || j && k");
    printf("--> value  is %d\n",i || j && k);

    printf("expression is %s\n","i || j && callthis()");
    printf("--> vnext  is %d\n",i || j && callthis());
}

int callthis()
{
    printf("--> The function is called.\n");

    return  1;
}

When compiled and run, the output was:

expression is i || j && k
--> value  is 1
expression is i || j && callthis()
--> vnext  is 1


The point I'm making is:  The Good Book (K&R II) says that 
"logical and" (&&) has a *higher* precedence that "logical or" 
(||).  And that same Good Book says that a series of "logical and" 
and "logical or" operations will occur left to right.

And so it is... left to right evaluation.  But *how* then does the 
"logical and" have a *higher* precedence than "logical or"... when 
this is ignored in the series of AND's and OR's???

(Yes, yes... I know about "short circuit" operators.  But I 
thought this was operator by operator.  In other words, if in the 
"i || j && callthis()", *if* the AND were done first [it's 
*not*]... and if "j" were FALSE, callthis() would *not* be done.

The actual evaluation is "i || j" done first... if "i" is TRUE, 
the the *entire* rest of the expression is short circuited.

Can someone explain how "&&" can have a *higher* precedence than 
"||"... when the series of AND's and OR's are just going to be 
done left to right???

I *thought* I understood C pretty well... guess not.

-- 

numerist at aquaporin4 dot com

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


Thread

Precedence Misunderstanding Charles Richmond <frizzle@aquaporin4.com> - 2012-07-16 23:18 -0500
  Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-16 21:54 -0700
    Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-16 22:13 -0700
      Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-16 22:36 -0700
        Re: Precedence Misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-17 13:57 +0100
    Re: Precedence Misunderstanding "Charles Richmond" <numerist@aquaporin4.com> - 2012-07-17 13:35 -0500
      Re: Precedence Misunderstanding "io_x" <a@b.c.invalid> - 2012-07-17 20:48 +0200
      Re: Precedence Misunderstanding Robert Wessel <robertwessel2@yahoo.com> - 2012-07-17 13:45 -0500
      Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-17 12:27 -0700
        Re: Precedence Misunderstanding Willem <willem@toad.stack.nl> - 2012-07-17 20:20 +0000
      Re: Precedence Misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-17 20:34 +0100
        Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-17 12:39 -0700
          Re: Precedence Misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-17 20:57 +0100
        Re: Precedence Misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-17 20:48 +0100
          Re: Precedence Misunderstanding Philip Lantz <prl@canterey.us> - 2012-07-17 21:12 -0700
            Re: Precedence Misunderstanding Ike Naar <ike@iceland.freeshell.org> - 2012-07-18 07:14 +0000
              Re: Precedence Misunderstanding "BartC" <bc@freeuk.com> - 2012-07-18 10:11 +0100
                Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-18 09:18 -0700
                Re: Precedence Misunderstanding Rui Maciel <rui.maciel@gmail.com> - 2012-07-18 18:10 +0100
                Re: Precedence Misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-18 18:27 +0100
                Re: Precedence Misunderstanding Rui Maciel <rui.maciel@gmail.com> - 2012-07-18 18:53 +0100
                Re: Precedence Misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-18 21:07 +0100
                Re: Precedence Misunderstanding Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-19 20:47 +0300
              Re: Precedence Misunderstanding Philip Lantz <prl@canterey.us> - 2012-07-18 23:54 -0700
                Re: Precedence Misunderstanding Ike Naar <ike@sverige.freeshell.org> - 2012-07-19 07:21 +0000
      Re: Precedence Misunderstanding "BartC" <bc@freeuk.com> - 2012-07-17 20:42 +0100
      Re: Precedence Misunderstanding Willem <willem@toad.stack.nl> - 2012-07-17 20:38 +0000
        Re: Precedence Misunderstanding Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-19 20:54 +0300
          Re: Precedence Misunderstanding Willem <willem@toad.stack.nl> - 2012-07-19 22:02 +0000
      Re: Precedence Misunderstanding pete <pfiland@mindspring.com> - 2012-07-17 22:31 -0400
      Re: Precedence Misunderstanding Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-07-18 00:35 -0700
    Re: Precedence Misunderstanding Rui Maciel <rui.maciel@gmail.com> - 2012-07-17 22:16 +0100
      Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-17 14:22 -0700
        Re: Precedence Misunderstanding Rui Maciel <rui.maciel@gmail.com> - 2012-07-18 16:35 +0100
          Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-18 10:03 -0700
            Re: Precedence Misunderstanding Rui Maciel <rui.maciel@gmail.com> - 2012-07-18 18:32 +0100
              Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-18 10:45 -0700
                Re: Precedence Misunderstanding Rui Maciel <rui.maciel@gmail.com> - 2012-07-18 19:29 +0100
                Re: Precedence Misunderstanding Ben Pfaff <blp@cs.stanford.edu> - 2012-07-18 11:45 -0700
                Re: Precedence Misunderstanding Keith Thompson <kst-u@mib.org> - 2012-07-18 13:26 -0700
                Re: Precedence Misunderstanding Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-07-19 00:51 -0700
              Re: Precedence Misunderstanding Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-07-19 00:11 -0700
  Re: Precedence Misunderstanding Robert Wessel <robertwessel2@yahoo.com> - 2012-07-17 00:05 -0500
  Re: Precedence Misunderstanding Ike Naar <ike@iceland.freeshell.org> - 2012-07-17 07:40 +0000

csiph-web