Difference between %e, %f and %g
What is %e, %f and %g?
%e, %f and %g are known as format specifiers in C programming language, these specific format specifiers are used to work with a decimal type of data
Which can be stored in double and float data type in c, hence %e, %f and %g are used to accept and print double and float datatype in C programming language.
Difference between %e, %f and %g specifier in C
All the three format specifiers %e, %f and %g are used to work with float and double data types in C, although there is a slight difference in the working of the three format specifiers
Let’s see how these work with examples below –
For example,
123.45, it will print it as 1.234500e+02
123.45 * 10^8, it will print it as 1.234500e+10
0.0000012345, it will print it as 1.234500e-06
For example,
123.45, it will print it as 123.450000
123.45 * 10^8, it will print it as 12345000000.000000
0.0000012345, it will print it as 0.000001
In the last example we lost all the bits after the 6th as precision for %f(float) is up to 6 decimals
Also removes succeeding zeros.
Example 1:
123.45, it will print it as 123.45
%f : 123.450000 & %e : 1.234500e+02
After removing succeeding zeros
%f : 123.45 & %e : 1.2345e+02
%f gives shorted result. So, %g prints 123.45
Example 2:
123.45 * 10^8, it will print it as 1.2345e+10
%f : 12345000000.000000 & %e : 1.234500e+10
After removing succeeding zeros
%f : 12345000000 & %e : 1.2345e+10
%e gives shorted result. So, %g prints 1.2345e+10
Using these format specifiers you can print your data simply in different formats, depending upon your needs.
Let’s see an example and learn how to use %e, %f and %g in a c code –
C Code:
#include <stdio.h> int main() { double ans = 123.45; printf("Printing using %%f %f\n",ans); printf("Printing using %%e %e\n",ans); printf("Printing using %%g %g\n\n\n",ans); // ans = 123.45e8; // scientific way of writing 123.45 * 10^8 printf("Printing using %%f %f\n",ans); printf("Printing using %%e %e\n",ans); printf("Printing using %%g %g\n\n\n",ans); ans = 123.45e-8; // scientific way of writing 123.45 * 10^(-8) i,e. 0.0000012345 // %f has upto 6 digits precision // 0.0000012345 converted to 0.000001 (max 6 precision allowed) printf("Printing using %%f %f\n",ans); printf("Printing using %%e %e\n",ans); printf("Printing using %%g %g\n",ans); return 0; }
Output:
Printing using %f 123.450000 Printing using %e 1.234500e+02 Printing using %g 123.45
Printing using %f 12345000000.000000 Printing using %e 1.234500e+10 Printing using %g 1.2345e+10 Printing using %f 0.000001 Printing using %e 1.234500e-06 Printing using %g 1.2345e-06
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
printf(” Printing the value of a using %%f %f\n “,a);
printf(” Printing the value of a using %%e %e\n”,a);
printf(” Printing the value of a using %%g %g\n”,a);
printf(” Printing the value of a using %%E %E\n”,a);
printf(” Printing the value of a using %%G %G\n”,a);
printf(” Printing the value of a using %%.2f %.2F\n”,a);
printf(“Printing the value of a using %%.2f %.2F\n”,a);
print a wanna answer