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
The %f format string
To begin with, suppose, we have a float variable a=32 and, 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?
The answer is 32.000000
But wait!
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,
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.200000
Here, the 200000 is the default 6-fraction digits
But why
Because we right-shifted the decimal point by one digit right
Number | exponential notation | scientific notation |
---|---|---|
1000 | 1e3 | 1×103 |
1000000 | 1![]() | 1x![]() |
1000000000 | 1![]() | 1x![]() |
The %g format string
The %g format string can display the floating point variable in either exponential format or fixed decimal format
But, 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=4x=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
Here, the % g format string is not displaying the trailing zeros of 32
However, the %f format string can display the trailing zeros of 32.000000
Therefore, the %f format string can display 32.23 as 32.230000 but the %g format string can display 32.23 as 32.23(the trailing zeros are neglected)
Furthermore, you can print the trailing zeros and trailing decimal points with %g format string by adding the #flag
float a=32.0f;
printf("%#g",a); // the result is 32.0000
Later, 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
To read and print the double value with %g format string, use l(ell) with scanf for %g format string(%lg), but, 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);
In C99 to read and write the 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);
Instead, if you use the C89 version of C then the effect of using conversion specifications %g and %G is undefined but, it is legal in C99
Leave a Reply