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

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:

Run
#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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

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