Given that x = 2, y = 1, and z = 0, what will the following statement display?
printf("answer = %d\n", (x || !y && z));
It was on a quiz and I got it wrong, I don’t remember my professor covering this, someone enlighten me please… I know the answer I get is 1, but why?
The expression is interpreted as
x || (!y &&z)(check out the precedence of the operators||,!and&&.||is a short-circuiting operator. If the left operand is true (in case of||) the right side operand need not be evaluated.In your case
xis true, so being a boolean expression the result would be 1.EDIT.
The order of evaluation of
&&and||is guaranteed to be from left to right.