neethu
asked on
precedence of && and ||
main()
{
int x,y,z;
x=y=z=1;
++x||++y&&++z;
printf("x=%d y=%d z=%d \n",x,y,z);
}
answer is
x=2 y=1 z=1
why is this happening.
++y&&++z should be evaluated first according to precedence.so the answer should be
x=1 y=2 z=2 right ,applying short circuit evaluation.
i will be grateful if u give me a detailed answer and some links about learning h
this,
love
neethu
{
int x,y,z;
x=y=z=1;
++x||++y&&++z;
printf("x=%d y=%d z=%d \n",x,y,z);
}
answer is
x=2 y=1 z=1
why is this happening.
++y&&++z should be evaluated first according to precedence.so the answer should be
x=1 y=2 z=2 right ,applying short circuit evaluation.
i will be grateful if u give me a detailed answer and some links about learning h
this,
love
neethu
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.hh.se/stud/d98rolb/ansi/c_operator_precedence.html
hmm according to this doc,, u r right.. && is above ||
let me have a closer look
hmm according to this doc,, u r right.. && is above ||
let me have a closer look
Akshay
Right on target ,good stuff
chiku
Right on target ,good stuff
chiku
akshay
What i think is
first && only get resloved
>> ++x||++y&&++z;
then
experssion will check for left operand of &&
that is
>>++x||++y
then here again left operand of ||
++x will be evaluvated
which is will preincremented which is true hence othe variable will get evaluvated
so x=2,y=1 and z=1
what do u say abt my explaination
chiku
What i think is
first && only get resloved
>> ++x||++y&&++z;
then
experssion will check for left operand of &&
that is
>>++x||++y
then here again left operand of ||
++x will be evaluvated
which is will preincremented which is true hence othe variable will get evaluvated
so x=2,y=1 and z=1
what do u say abt my explaination
chiku
Neethu
check out of explaination can help u in problem
chiku
check out of explaination can help u in problem
chiku
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sunnycoder
is my analysis is correct what i gave?
chiku
is my analysis is correct what i gave?
chiku
chiku ... I am afraid this is not correct
>experssion will check for left operand of &&
>that is
>>++x||++y
If that were the case, the other operand to && would have also been evaluated which clearly has not been done
as per C standard
The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result.
and the final result of the statement is true... which the compiler has generated ...
if a compiler evaluates all the expressions and produces result true, it will be called correct
", it is a common misperception that precedence and associativity rules dictate (rather than merely influence) order of expression evaluation, the order in which operators are applied to their operands. "
http://www-pat.fnal.gov/personal/wb/boost/ISOcxx/doc/Expressions.html
>experssion will check for left operand of &&
>that is
>>++x||++y
If that were the case, the other operand to && would have also been evaluated which clearly has not been done
as per C standard
The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result.
and the final result of the statement is true... which the compiler has generated ...
if a compiler evaluates all the expressions and produces result true, it will be called correct
", it is a common misperception that precedence and associativity rules dictate (rather than merely influence) order of expression evaluation, the order in which operators are applied to their operands. "
http://www-pat.fnal.gov/personal/wb/boost/ISOcxx/doc/Expressions.html
In short, the evaluation above is correct ...
The only binding on any standard compiler would be to produce the end result consistently and correctly which it did ... another compiler may evaluate all of the subexpressions and produce the same results... that behaviour cannot be labelled as incorrect either...
you may get different values on a different compiler
use of such expressions is always dangerous and leads to hard to maintain and hard to understand type of code
The only binding on any standard compiler would be to produce the end result consistently and correctly which it did ... another compiler may evaluate all of the subexpressions and produce the same results... that behaviour cannot be labelled as incorrect either...
you may get different values on a different compiler
use of such expressions is always dangerous and leads to hard to maintain and hard to understand type of code
If && had an absolute higher precedence over ||, then the expression ++y&&++z would have been evaluated first, but I dont think that is true. As far as I know, both && and || have equal precedence and as sunnycoder said, the compiler is free to decide on the order as long as the final result is consistent.
>>>As far as I know, both && and || have equal precedence and as sunnycoder said, the compiler is free to decide on the order as long as the final result is consistent.
NO .. this expression A || B && C is not at all ambigous.. and hence compiler is not free to decide on the order ..compiler here is prefectly following the precedence of && over ||... and all compilers will give same results as long as above is not bracketed in special way ( look below second case)..
&& is not at same level as ||
here is detailed explaination for the outcome that u observed.
case 1. assume && has precendece above || , the expression is evaluated as if it is brackets are like
A || (B && C).
the parse tree would look like
||
A &&
B C
now evaluation starts from left subtree of root node ||, and because of the short circuit property of || since A is already true, right subtree isnt touched at all.
case 2: || has precedence over &&, then the expression is evaluated as if it is brackets are like
(A || B) && C
the parse tree would look like
&&
|| C
A B
now again first left subtree is evaluated, of which going further down towards left subtree.. A is evaluated first.. now due to short circuit nature of A, B is not evaluated since A is already true.. NOW moving up.. due to short circuit property of && C must be evaluated in this case.. ( since if C comes out to be 0 end result will be 0)
You can verify this by changing ur expression to
(++x || ++y) && ++z;
with this change ur program will give x=2,y=1,z=2
case 3: && and || are same precedence..
NO NO NO NO.. ( sorry i also first made that assumption , but this realisation came to me when i came back home, heavily drunk :- ) )
if this is the case, compiler cannot generate a proper parse tree. OR it will have to make assumptions and that will be the real ambiguous issue.
i have also made a comment in the other copy of the question, so that the correct explaination goes to all ppl involved here.
hope i made my point clear
akshay
NO .. this expression A || B && C is not at all ambigous.. and hence compiler is not free to decide on the order ..compiler here is prefectly following the precedence of && over ||... and all compilers will give same results as long as above is not bracketed in special way ( look below second case)..
&& is not at same level as ||
here is detailed explaination for the outcome that u observed.
case 1. assume && has precendece above || , the expression is evaluated as if it is brackets are like
A || (B && C).
the parse tree would look like
||
A &&
B C
now evaluation starts from left subtree of root node ||, and because of the short circuit property of || since A is already true, right subtree isnt touched at all.
case 2: || has precedence over &&, then the expression is evaluated as if it is brackets are like
(A || B) && C
the parse tree would look like
&&
|| C
A B
now again first left subtree is evaluated, of which going further down towards left subtree.. A is evaluated first.. now due to short circuit nature of A, B is not evaluated since A is already true.. NOW moving up.. due to short circuit property of && C must be evaluated in this case.. ( since if C comes out to be 0 end result will be 0)
You can verify this by changing ur expression to
(++x || ++y) && ++z;
with this change ur program will give x=2,y=1,z=2
case 3: && and || are same precedence..
NO NO NO NO.. ( sorry i also first made that assumption , but this realisation came to me when i came back home, heavily drunk :- ) )
if this is the case, compiler cannot generate a proper parse tree. OR it will have to make assumptions and that will be the real ambiguous issue.
i have also made a comment in the other copy of the question, so that the correct explaination goes to all ppl involved here.
hope i made my point clear
akshay
>and hence compiler is not free to decide on the order
yes it is free to decide the order in context of the entire expression ... only binding is that all sequence points results should be correct and consistent ...
>..compiler here is prefectly following the precedence of && over ||...
as I quoted from a page "it is a common misperception that precedence and associativity rules dictate (rather than merely influence) order of expression evaluation, the order in which operators are applied to their operands. "
>and all compilers will give same results as long as above is not bracketed in special way
Yes and no ... all standard C compilers will evaluate the expression to true but you cannot place guarantees on the resulting values of x y and z
yes it is free to decide the order in context of the entire expression ... only binding is that all sequence points results should be correct and consistent ...
>..compiler here is prefectly following the precedence of && over ||...
as I quoted from a page "it is a common misperception that precedence and associativity rules dictate (rather than merely influence) order of expression evaluation, the order in which operators are applied to their operands. "
>and all compilers will give same results as long as above is not bracketed in special way
Yes and no ... all standard C compilers will evaluate the expression to true but you cannot place guarantees on the resulting values of x y and z
if you intended to say that, a compiler may choose to evaluate from right to left.. then i would agree..
but my point is that here precedence of && over || is playing its role perfectly..
in my comment in the other copy of this thread,the difference three cases of precendence is clearly visible
sorry all the three cases are discussed inthis thread only :)
Sunny
In that sence this is entirely compiler dependent??????????
correct me if i'm wrong.
-chiku
In that sence this is entirely compiler dependent??????????
correct me if i'm wrong.
-chiku
The values of x, y and z - yes they are entirely compiler dependent
I can have a compiler which will evaluate both operands of && , see that the result is true and short circuit ++x ...
as we have seen, gcc chose to short circuit the entire && sub expression ... so it all depends on the compiler
The value of expression - every corrent compiler will evaluate it to true
I can have a compiler which will evaluate both operands of && , see that the result is true and short circuit ++x ...
as we have seen, gcc chose to short circuit the entire && sub expression ... so it all depends on the compiler
The value of expression - every corrent compiler will evaluate it to true
No comment has been added lately and this question is therefore classified abandoned.
If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp
Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with 100:25 split between sunnycoder and akshayxx
Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sunny
EE Cleanup Volunteer
If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp
Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with 100:25 split between sunnycoder and akshayxx
Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sunny
EE Cleanup Volunteer
>>but && has more precedence than || right
i dont think so. both have equal precedence.. the expression is evaluated from left to right