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
πŸ’¬ Discuss
βœ… 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);
Explanation by: Mr. Dubey

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);

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
221
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Akash Lawaniya
Publisher
πŸ“ˆ
80%
Success Rate