C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - Find Output of Program (Q.No. 9)
What will be the output of the program?
int main()
    float d=2.25;
    printf("%e,", d);
    printf("%f,", d);
    printf("%g,", d);
    printf("%lf", d);
    return 0;
2.2, 2.50, 2.50, 2.5
2.2e, 2.25f, 2.00, 2.25
2.250000e+000, 2.250000, 2.25, 2.250000
Answer: Option

printf("%e,", d); Here '%e' specifies the "Scientific Notation" format. So, it prints the 2.25 as 2.250000e+000.

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

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

printf("%lf,", d); Here '%lf' specifies the "Long Double" format. So, it prints the 2.25 as 2.250000.

8 comments Page 1 of 1.

Rohit mundhe said:   4 years ago
math.h is not included. How the program will run?

Harshad said:   8 years ago
Any words are remaining than tell me about it?

Kritika said:   9 years ago
For long double float we simply write %l whereas %lf is not allowed as said in previous answers so how could option C be the answer?

Ripal said:   10 years ago
Why answer B is not correct? when you are using %f then it will print 2.25f in this program.

Kaushik said:   1 decade ago
float a=4.24l; /*l is for long double in declaration and printf("%Lf",a); defination of a*/
} /*I am getting some weird result*/

Dimple kamboj said:   1 decade ago
What is use of %g and %lf?

Raghav Naganathan said:   1 decade ago
@Rahul...%g is used for removing the insignificant zeroes after the mantissa of a decimal number.

eg. 2.250000 is reduced to 2.25.

what it basically does is it gives the decimal number without the insignificant zeroes.

Hope this helps.

Rahul said:   1 decade ago
Tell me more about %g.

Post your comments here:

Your comments will be displayed after verification.