the output of following code is 0010.
https://codeshare.io/5wRBeK
but according to me its should be 0011.
the expression should be evaluated as
int c = a || (–b)
int d = (a–) && (–b)
according to precedence table
where am I wrong ?
the output of following code is 0010.
https://codeshare.io/5wRBeK
but according to me its should be 0011.
the expression should be evaluated as
int c = a || (–b)
int d = (a–) && (–b)
according to precedence table
where am I wrong ?
Output must be 0010.
Initial values of a and b are 1.
Since a is 1, the expression --b is not executed because of the short-circuit property of logical or operator
So c becomes 1
a and b remain 1
Now,
int d = (a–) && (–b)
The post decrement operator – returns the old value in current expression so first operand of logical and is 1, shortcircuiting doesn’t happen here. So the expression --b is executed and --b returns 0 because it is pre-increment.The values of a and b become 0, and the value of d also becomes 0.
This way output comes as 0010
but according to precedence if there are more than one operators in an expression than the operator with highest precedence is evaluated first, so technically post decrement should be evaluated first as it has higher precedence than logical operators and therefore b should be decremented in expression int c = a || --b and after evalutaion b should be 0 and a should be 1
Check this. Read about short circuit operators.