Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Willem <willem@toad.stack.nl> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Precedence Misunderstanding |
| Date | 2012-07-17 20:38 +0000 |
| Organization | Stack Usenet News Service |
| Message-ID | <slrnk0bjai.1frm.willem@toad.stack.nl> (permalink) |
| References | <ju2p36$qj9$1@dont-email.me> <874np7j8br.fsf@blp.benpfaff.org> <ju4ba5$lso$1@dont-email.me> |
Charles Richmond wrote:
) The crux of what I do *not* understand is this:
)
) Can someone explain how "&&" can have a *higher* precedence than
) "||"... when any series of AND's and OR's are just going to be
) done left to right???
The precedence determines how the results *will be used*.
It does not determine the order in which they are evaluated.
) For example, if you have a long series of logical AND's and OR's like this:
)
) a && b || c || d && x && y || z
)
) How will "&&" having a higher precedence than "||" effect this at all???
This is, step by step, what the program will do:
/* Construct for the expression a && b || c || d && x && y || z */
int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
{
if (a) {
if (b) {
return 1;
}
}
if (c) {
return 1;
}
if (d) {
if (x) {
if (y) {
return 1;
}
}
}
if (z) {
return 1;
}
return 0;
}
Now, as you can see: a, b, c, d, x, y, z are evaluated in that order.
Suppose that '&&' had higher precedence, it would become the following:
/* Construct for the expression a && b || c || d && x && y || z
* with precedences reversed */
int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
{
if (!a) {
return 0;
}
if (!b) {
if (!c) {
if (!d) {
return 0;
}
}
}
if (!x) {
return 0;
}
if (!y) {
if (!z) {
return 0;
}
}
return 1;
}
As you can see, they are still evaluated in the same order, yet still they
are grouped differently.
) Create any series of AND's and OR's you want, and please show me how the
) precedence of "&&'" and "||" will make any difference under the current C
) standard.
See above.
) I can *not* understand how "&&" can have a higher precedence than "||"...
) when it seems to make *no* difference in *any* expression I've seen.
int a() { printf("a=0\n"); return 0; }
int b() { printf("b=1\n"); return 1; }
int c() { printf("c=1\n"); return 1; }
int main() {
printf("First\n");
printf("Result: %d\n", (a() && b()) || c());
printf("Second\n");
printf("Result: %d\n", a() && (b() || c()));
return 0;
}
There you go.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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