A

Admin • 833K Points
Coach

Q. What will be the output of the following C code?

Code:
#include <stdio.h>
int main()
{
float fl = 15.621212121212;
printf("%f", fl);
}
  • (A) 15.621212121212
  • (B) Compilation Error
  • (C) Garbage value
  • (D) 15.621212
  • Correct Answer - Option(D)
  • Views: 21
  • Filed under category C Programming
  • Hashtags:

Explanation by: Admin

Floating-Point Precision:

  • In C, the float data type typically provides only 6–7 decimal places of precision.
  • The given number 15.621212121212 has more than 7 decimal places.
  • Since fl is declared as a float, the extra decimal places get truncated.

printf("%f", fl); Behavior:

  • By default, %f prints floating-point numbers with 6 decimal places.
  • So, the output will be 15.621212 (truncated after 6 places).

Correct Answer:

(D) 15.621212

Bonus Notes:

  • If you want more decimal places, use double instead of float:
  • To control the decimal places in printf, use:This will print 12 decimal places (if precision allows).
printf("%.12f", fl);
double fl = 15.621212121212;
printf("%lf", fl);

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.