LearnFrenzy
Sign in Take a free test

C Library Functions · Predict the Output · Question 2

div() with positive and negative numbers Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    div_t a = div(17, 5), b = div(-17, 5);
    printf("%d %d %d %d\n", a.quot, a.rem, b.quot, b.rem);
    return 0;
}
  • A 3 2 -3 -2 Correct
  • B 3 2 -4 3
  • C 3 2 -3 2
  • D 3 2 -4 -2
Correct Answer: Option A

Integer division truncates toward zero, so −17 / 5 is −3 and the remainder takes the sign of the dividend: −2.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links