Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?   

 int main()
    {
        int x = 2, y = 2;
        x /= x / y;
        printf("%d\n", x);
        return 0;
    }

 

  • A.2
  • B.1
  • C.0.5
  • D.Undefined behaviour

Answer: A

Correct answer: 2

x /= x / y; is x = x / (x / y). The right-hand side is computed first, with integer division throughout:

\[ x / y = 2 / 2 = 1, \quad x = 2 / 1 = 2 \]

So x is unchanged at 2.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.