Q. What will be the output of the following C code?
Code:#include <stdio.h>
int main()
{
float fl = 15.621212121212;
printf("%f", fl);
}
β
Correct Answer: (D)
15.621212
Explanation:
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);