LearnFrenzy
Sign in Take a free test

Floating Point Issues · Predict the Output · Question 4

float r = 5 / 2 – output Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    float r = 5 / 2;
    printf("%.2f %.2f\n", r, 5 / 2.0f);
    return 0;
}
  • A 2.50 2.50
  • B 2.00 2.50 Correct
  • C 2.00 2.00
  • D 2.50 2.00
Correct Answer: Option B

5 / 2 is integer division (2) before it is stored in the float. Only when an operand is floating, as in 5 / 2.0f, is the result 2.5.

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