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

Discussion

What is the output of this C code?    

void main()
    {
        int a = 5, b = -7, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d%d%d%d", a,  b, c, d);
    }

 

  • A.6  -6  0  0
  • B.6  -5  0  1
  • C.-6  -6  0  1
  • D.6  -6  0  1

Answer: D

Correct answer: 6 -6 0 1

Evaluate left to right, remembering that any non-zero value is true.

  • ++a makes a 6, which is true.
  • ++b makes b -6 - non-zero, so also true.
  • The && is therefore true, so || short-circuits and ++c never runs: c stays 0.
  • d gets the value of the whole expression, 1.

Output: 6 -6 0 1.

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

Post your comment

Please log in or register to post a comment.