LearnFrenzy
Sign in Take a free test

Floating Point Issues · Predict the Output · Question 9

(int) -3.7 vs floor(-3.7) – output Answer, explanation & discussion

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

What is the output of the following C program?

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

int main(void) {
    printf("%d %d %.1f\n", (int) -3.7, (int) 3.7, floor(-3.7));
    return 0;
}
  • A -4 3 -4.0
  • B -3 3 -4.0 Correct
  • C -3 4 -3.0
  • D -4 4 -4.0
Correct Answer: Option B

Converting to int truncates toward zero, so −3.7 becomes −3. floor() rounds down, toward −∞, giving −4. They agree only for positive numbers.

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