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

Discussion

What will be the output of

int x=5; printf("%d", x++);

  • A.5
  • B.6
  • C.4
  • D.Error

Answer: A

The output is 5.

x++ is the post-increment operator: the expression's value is the current value of x, and x is increased only afterwards. So printf receives 5 and prints 5, and x becomes 6 once the statement is done.

6 would be printed by the pre-increment ++x, which increments first and then uses the new value. 4 would need a decrement, and the code is valid C, so there is no error.

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

Post your comment

Please log in or register to post a comment.