C
Charles Richmond
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.
#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.