Difference between %e, %f and %g | g in c | f in c | Difference Between g and g

1051
g in c
g in c

Difference between %e, %f and %g | g in c | f in c | Difference Between g 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 –%e%e represents the data in exponential power(scientific format), ‘e’ would be by default mean exponential power 10.

Format Specifier For Double | Double Format Specifier In C | Means In C Programming

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%f%f represents the data in normal decimal form, upto six decimal places, although you can control, that upto how many decimal places did you want your output. 

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%g%g represents the decimal format of the answer, depending upon whose length is smaller, comparing between %e and %f.

Also removes succeeding zeros. 

1 F In C | G And G Difference | 1f In C | 2 F In C | What Is The Difference Between G And G

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

CyberTecz Jobs is Available on Play Store, Download Now & Get an Inside Look into Jobs: Click here

Wipro Interview Questions for Technical Round: Click here

HR Interview Questions of Wipro: Click here

How To Write Resume For Getting Shortlisted In Wipro: Click here

Wipro Mock Test with Aptitude and Coding Assessment: Click here

Wipro Recruitment Process, Test, and Exam Pattern: Click here

Join Telegram Group of Daily Jobs Updates for 2010-2023 Batch: Click Here

Abroad Jobs Opportunities for Professionals & Freshers, Apply Now: Click here

If You Want To Get More Daily Such Jobs Updates, Career Advice Then Join the Telegram Group From Above Link Also Press Red Bell Icon At The Left Side of Page To Subscribe our Updates.

Netflix Careers 2023 Hiring Freshers as Software Engineers for Los Gatos, California: Click here

How To Introduce Yourself In an Interview, Professional Advice For Job Seekers: Click here

Microsoft, Google, Cognizant, Capgemini, Syntel, Top MNC Companies Hiring Freshers: Click here

Why You’re Not Getting Response From Recruiter?: Click here

Top 5 High Salary Jobs in India IT Sector 2021: Click here

Ways To Find Peace of Mind and Inner Calm In Today’s Stressful Life: Click here

A Leadership Guide For How To Win Hearts and Minds: Click here

How To Improve Communication Skills with 12 Strategy: Click here

Career Tips for Freshers: Top 7 Hacks To Land Your Target JobClick here

Which Graphics Processor is Best for Gaming 2021?: Click here

Feel Like Demotivated? Check Out our Motivation For You: Click here

Top 5 Best Mobile Tracking App in 2021 For Mobile & PC: Click here

5 Proven Tips For How To Look Beautiful and Attractive: Click here