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

Discussion

What is the output of this C code?

int main()
    {
        int i = 2;
        int j = ++i + i;
        printf("%d\n", j);
    }

 

  • A.6
  • B.5
  • C.4
  • D.Compile time error

Answer: A

Correct answer: 6

++i takes i to 3 and contributes 3, and the second operand then reads 3, giving \(3 + 3 = 6\). As with any expression that both modifies and reads the same variable without an intervening sequence point, this is formally undefined behaviour; 6 is simply what the common compilers give.

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

Post your comment

Please log in or register to post a comment.