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

Discussion

What is the output of this C code?    

void main()
    {
        int k = 8;
        int x = 0 == 1 && k++;
        printf("%d%d\n", x, k);
    }

 

  • A.0   9
  • B.0   8
  • C.1   9
  • D.1   8

Answer: B

Correct answer: 0 8

&& short-circuits: if the left operand is false the right is never evaluated. 0 == 1 is false, so k++ never runs and k stays 8, while x gets the value 0. The output is therefore 0 8 - printed as 08, since the format string has no separator.

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

Post your comment

Please log in or register to post a comment.