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 = 1;
        x *= x + y;
        printf("%d\n", x);
        return 0;
    }

 

  • A.5
  • B.6
  • C.Undefined behaviour
  • D.Compile time error

Answer: D

Correct answer: 6

x *= x + y; means x = x * (x + y) - the whole right-hand side is evaluated first, because compound assignment binds more loosely than +:

\[ x = 2 \times (2 + 1) = 6 \]

There is exactly one modification of x, so nothing here is undefined.

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

Post your comment

Please log in or register to post a comment.