Given that x = 2, y = 1 and z = 0, what will be the next statement of the mapping?
printf ("answer =% d \ n", (x ||! y && z));
Good - feeling a little guilty for severe mistakes in the incorrect wording of the question, so I will try to help you differently with regard to other answers ... :-)
When you have such a question, divide it into manageable pieces.
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"
The output has everything you need to determine what happens:
true == 1 shows how C / C ++ converts true logical expressions into integral value 1 for printf, regardless of the values โโappearing in a boolean expressionfalse == 0 shows how C / C ++ converts false expressions to "0"(!y) == 0 because! is not a logical operator, but C / C ++ considers that 0 is the only integral value corresponding to false, while all the others are true, therefore !1 == !true == false == 0(x || !y) == 1 , and you know that !y is 0, so substituting the known values โโand simplifying: (2 || 0) == 1 equivalent to (true or false) == true ... clear as a logical rule(!y || z) == 0 - substitution of known values: (0 || 0) == (false or false) == false == 0(x || !y && z) == 1 : here is a crunch! From above we know:x || !y x || !y is 1 / true, which, if relevant, implies 1 / true && z / 0 / false == 1 / true <- this obviously makes no sense, so there should be no way to calculate the C / C ++ answer!- (! y && z) is false, which, if it matters, means x / 2 / true || false == 1 / true <- this is true, so it must be implicit order.
Thus, we got the priority of the operator - the evaluation order || and && operators, from the results displayed by the compiler, and saw that if and only if && is evaluated to || then we can understand the meaning of the results.
Tony delroy
source share