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

Discussion

What is the output of this C code?    

int main()
    {
        int a = 10, b = 10;
        if (a = 5)
        b--;
        printf("%d, %d", a, b--);
    }

 

  • A.a = 10, b = 9
  • B.a = 10, b = 8
  • C.a = 5, b = 9
  • D.a = 5, b = 8

Answer: C

Correct answer: a = 5, b = 9

if (a = 5) assigns 5 to a, and the value of that assignment, 5, is non-zero and so true. The body runs, taking b from 10 to 9. The printf then shows a as 5 and, because b-- is a post-decrement, the value passed is the current 9 - the decrement to 8 happens after the value has been handed over, and nothing prints afterwards.

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

Post your comment

Please log in or register to post a comment.