LearnFrenzy
Sign in Take a free test

Floating Point Issues · Predict the Output · Question 3

Storing 16777217 in a float – output Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    float f = 16777217;
    printf("%.0f\n", f);
    return 0;
}
  • A 16777217
  • B 16777218
  • C 1.67772e+07
  • D 16777216 Correct
Correct Answer: Option D

2²⁴ + 1 needs 25 significant bits, but a float has 24. It is rounded to the nearest representable value, 16 777 216 (a tie, broken towards the even mantissa). Compilers even warn that the constant changes value.

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