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.

thumbs-up

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 –

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

One comment on “Difference between %e, %f and %g”


  • Himani

    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