Question 4
Back to Exercise
What is the output of the following C program?
#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!