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

Discussion

What is the output of this C code?    

int main()
    {
        int i = -5;
        int k = i %4;
        printf("%d\n", k);
    }

 

  • A.Compile time error
  • B.-1
  • C.1
  • D.None

Answer: B

Correct answer: -1

In C the result of % takes the sign of the dividend, and the language requires that (a/b)*b + a%b == a. Since -5 / 4 truncates toward zero to \(-1\), we need \(-1 \times 4 + r = -5\), giving \(r = -1\). So the program prints -1.

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

Post your comment

Please log in or register to post a comment.