C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#include<stdio.h>
int main()
{
    float f=43.20;
    printf("%e, ", f);
    printf("%f, ", f);
    printf("%g", f);
    return 0;
}
4.320000e+01, 43.200001, 43.2
4.3, 43.22, 43.21
4.3e, 43.20f, 43.00
Error
Answer: Option
Explanation:

printf("%e, ", f); Here '%e' specifies the "Scientific Notation" format. So, it prints the 43.20 as 4.320000e+01.

printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 43.20 as 43.200001.

printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2.

Discussion:
6 comments Page 1 of 1.

Vivek dutt said:   6 years ago
Why it is 43.2000001?
(2)

Puja said:   7 years ago
Why 43.2000001?
(1)

Asha said:   1 decade ago
I cant understand this program. Can anyone explain how %e,%g,%f works in C and explain about it in detail ?
(1)

Sagar said:   1 decade ago
In the above problem

Why printf ("%f", f) ; Statement prints the 43.200001 other than 43.200000 ?

Shyam said:   1 decade ago
In the above problem

why printf ("%f", f) ; statement prints the 43. 200001 other than 43. 200000 ?

Saki__001 said:   1 decade ago
Can anybody explain how various notations works?

i.e %e, %f, %g and such all in details.

Post your comments here:

Your comments will be displayed after verification.