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

Discussion

What is the output of this C code?

void main()
    {
        int y = 3;
        int x = 7 % 4 * 3 / 2;
        printf("Value of x is %d", x);
    }

 

  • A.Value of x is 1
  • B.Value of x is 4
  • C.Value of x is 3
  • D.Compile time error

Answer: A

Correct answer: Value of x is 4

%, * and / all have the same precedence and associate left to right, so the expression is grouped as ((7 % 4) * 3) / 2:

\[ 7 \bmod 4 = 3, \quad 3 \times 3 = 9, \quad 9 / 2 = 4 \]

The final division is integer division, so the 0.5 is discarded and x is 4. The declaration of y is never used.

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

Post your comment

Please log in or register to post a comment.