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)
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 x
is 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.
If I'm not mistaken, it will print 1. (Let's assume short circuiting is off)
(x || !y && z)
or (true || !true && false)
will first evaluate the ! operator giving (true || false && false)
Then the &&: (true || false)
Then || : true
Printf will interpret true in decimal as 1. So it will print answer = 1\\n
Given that x = 2, y = 1, and z = 0, what will the following statement display?
printf("answer = %d\\n", (x || !y && z));
Ok - feeling a bit guilty for the harsh quip re poor wording of the question, so I'll try to help you in a different way to the other answers... :-)
When you've a question like this, break it down into manageable chunks.
Try:
int x = 2, y = 1, z = 0;
printf("true == %d\n", 10 > 2); // prints "1"
printf("false == %d\n", 1 == 2); // prints "0"
printf("!y == %d\n", !y); // prints "0"
printf("(x || !y) == %d\n", x || !y); // "1" - SEE COMMENTS BELOW
printf("(!y || z) == %d\n", !y || z); // "0"
printf("(x || !y && z) == %d\n", x || !y && z); // "1"
In the output there, you've got everything you need to deduce what's happening:
true == 1
reveals how C/C++ convert truthful boolean expressions to the integral value 1 for printf, irrespective of the values appearing in the boolean expression false == 0
reveals how C/C++ converts false expressions to "0" (!y) == 0
because ! is the logical not operator, and C/C++ consider 0 to be the only integral value corresponding to false, while all others are true, so !1 == !true == false == 0
(x || !y) == 1
, and you know !y
is 0, so substituting known values and simplifying: (2 || 0) == 1
is equivalent to (true or false) == true
... that's understandable as a logical rule (!y || z) == 0
- substituting known values: (0 || 0) == (false or false) == false == 0
(x || !y && z) == 1
: here's the crunch! From above, we know:
x || !y
x || !y
is 1/true, which if relevant would imply 1/true && z/0/false == 1/true <- this clearly doesn't make any sense, so it must not be the way C/C++ are calculating the answer! In this way, we've derived the operator precedence - the order of evaluation of the || and && operators, from the results that the compiler is displaying, and seen that if and only if && is valuated before || then we can make some sense of the results.
answer = 1
or maybe:
answer = -27
2 || !1 && 0
2 || 0 && 0
2 || 0
true
true = non-zero value
printf("answer = %d",*true*); -> who knows
Most compilers will output answer = 1
. I wouldn't confidently state that all compilers will do that though, but I am confident all compilers would return non-zero.
我不打算给你直接的答案,因为你可以编译并运行它,但问题只是测试你是否知道运算符优先级。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.