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

Discussion

What will be the output of the following C code?

void main() 
{ 
   div_t  res;  
   res = div(34, 4); 
   printf("quotient part = %d\n", res.quot); 
   printf("remainder part = %d\n", res.rem); 
}

 

  • A.
    quotient part=0
    remainder part=4
  • B.
    quotient part=8
    remainder part=2
  • C.
    quotient part=4
    remainder part=0
  • D.
    quotient part=2
    remainder part=8

Answer: B

Correct answer: quotient part=8 remainder part=2

div(34, 4) returns a div_t structure holding both results at once: quotient 8 and remainder 2, since \(34 = 8 \times 4 + 2\). Computing both together is cheaper than a separate / and %, and the sign behaviour is guaranteed to truncate toward zero.

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

Post your comment

Please log in or register to post a comment.