Why we use %g in C

Rendered by QuickLaTeX.com

To read an integer we use either %d and %i format stringS, but to read a float we use %e, %f, and  %g format strings of type double

to be sure,

of course, Let’s deconstruct some examples

The %f format string

to explain, Well, imagine this

To begin with, suppose, we have a float variable a=32 and, at that instant, we would like to print variable a with %f format string

 float a=32.0f;
    printf("%f",a); //result 32.000000

Guess what the output will be?

unquestionably, The answer is 32.000000

But wait!

at this instant, Why does the output have 6 fractional digits?

as can be seen, The %f format string will always display the floating point variable in a fixed decimal format which means it will always have a fraction part which is by default six (.000000)

with this intention,32 will become 32.000000,

in a word,

The %e format string

The %e format string will always display the floating point variable in exponential format

float a=32.0f;
    printf("%e",a);// the result is 3.200000e+001

as i have shown, if we use %e we get the answer as 3.200000e^{+001}

undoubtedly, the 200000 is the default 6-fraction digits

But why e^1

Because we right-shifted the decimal point by one digit right

Numberexponential notation   scientific notation
10001e31×103
10000001e^61x10^6
10000000001e^91x10^9

The %g format string

The %g format string can display the floating point variable in either exponential format or fixed decimal format  

At the same time, how %g decide to display exponential format or fixed decimal format?

at this point, It all depends upon the floating-point variable size

In short, it switches  to exponential format when a floating point variable exponent is less than -4(that is, 0.0004=4x10^{-4}=4e^{-4}) or greater than the precision(which is by default 6) such as[1234567]

Trailing zeros and trailing decimal point are not printed

float a=32.0f;
    printf("%g",a); // the result is 32

Have you noted, to point out, the % g format string is not displaying the trailing zeros of 32

whereas the %f format string can display the trailing zero of 32.000000 and on this occasion, it is not possible with the %g format string

until now, okay,

the %f format string can displays 32.23 as 32.230000 but the %g format string can displays the 32.23 as 32.23(the trailing zeros are neglected)

Furthermore, you can print the trailing zeros and trailing decimal point with %g format string only in case, when you use the #flag

float a=32.0f;
    printf("%#g",a); // the result is 32.0000

if the floating variable is too large or too small then, at that instant, the %g format string switches to the exponential format

so, that variable adjust itself with fewer characters

 float a=321224587.23f;
    printf("%g",a); // the result is 3.21225e+008

On one hand, To read and print the double value with %g format string

Use l(ell) with scanf for %g format string(%lg) and the l(ell) will not have any effect on printf function with %g format string

double a=32.23;
   double b;
printf("enter the value of B\n");
    scanf("%lg",&b);
    printf("VALUE of a= %g\n",a); // the result is 32.23
    printf("VALUE of b= %g\n",b);

On the other hand, in C99 to read and write the type of long double use the letter L in front of the %g format string

long double a=32.23;
   scanf("%Lf",&a);
    printf("VALUE of a= %Lg\n",a);

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.