Interview Question
Country: India
z=++x||++y&&++z;
In the above line precedence of && > precedence of ||, hence its equivalent to
z=++x||(++y&&++z );
But once the ++x evaluates to 2 i.e. true, no need to further process,
Hence the o/p 2 1 1
But tell me if unary operators have the highest precedence among all (actually second highest)....then why here we are bothering about "&&" or "||"......first unary operators should be evaluated.... according to ur explanation answer is right but how ??? why we are not evaluating unary operators first ????
second thing in denis and ritchie ANSI C it is given that if their are to expressions f(x) and g(x) in a bigger expression then its not fixed that which of the "f()" or "g()" will be evaluated first.........what exactly it means and if it is so then why we bother about precedence and associativity in evaluating an expression.... clarify it with example
i have the same doubt as deepak yadav. If we are calculating ++x first, then why not ++z and ++y
z=++x||++y&&++z
Short circuiting is fine.
I wanna ask that precedence of ++ is greater than =
so ++x will execute first then it will assigned to z
so z will be 2
Or this is the condition
z=++x||++y&&++z
here left of the || is taken as one component, as x is > 0 it skips the assignment left z as 1. after that z is incremented to 2.
Short Circuiting ....
- Anonymous June 29, 2012