How C ++ Operators Work

Given that x = 2, y = 1 and z = 0, what will the next operator display?

printf("answer = %d\n", (x || !y && z)); 

It was a quiz, and I realized what was wrong, I donโ€™t remember how my professor covered it, someone enlightened me, please ... I know that I get 1, but why?

+7
c ++ operators order-of-evaluation
source share
5 answers

The expression is interpreted as x || (!y &&z) x || (!y &&z) (check the priority of the || && and && operators.

|| is a short-circuiting operator . If the left operand is correct (in the case of || ), the right operand does not need to be evaluated.

In your case, x is true, therefore, being a Boolean expression, the result will be 1.

EDIT .

Evaluation Procedure && and || guaranteed to be left to right.

+3
source share

If I'm not mistaken, it will print 1. (Assume the short circuit is off)

(x || !y && z) or (true || !true && false) will evaluate first! operator giving (true || false && false)

Then the value &: (true || false)

Then ||: true

Printf interprets true in decimal as 1. Thus, it will print answer = 1\n

+1
source share

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 expression
  • false == 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.

0
source share
 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 print answer = 1 . I would not confidently state that all compilers will do this, although, but I am sure that all compilers will return a nonzero value.

-one
source share

I am not going to give you a direct answer, because you could just compile it and run it, but the question just checks to see if you know about operator priority.

-2
source share

All Articles