In C, is there any difference in the format specifiers %f, %e, %g, %E and %G when used to read into a float variable with scanf? That is, will the behaviour of the code snippet
float x;
scanf("%<one of f, e, g, E, G>", &x);
ever depend on the choice of the specifier?
I first supposed that %f would only interpret decimal notation correctly, and %e would only interpret scientific notation correctly, but on my system, each of them works fine in either case. But maybe my system is just liberal...
I couldn't find any definite statement about this in my books or on the web...
%e %e represents the data in exponential power(scientific format), 'e' would be by default mean exponential power 10. %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.
The short answer is that it has no impact on printf , and denotes use of float or double in scanf . For printf , arguments of type float are promoted to double so both %f and %lf are used for double . For scanf , you should use %f for float and %lf for double .
Many people confuse the %d and %f printf specifiers. Both %d and %f are valid when used with the Java printf function. However, only %f is intended for use with floating point values such as floats and doubles.
%g. It is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output. %p. It is used to print the address in a hexadecimal form.
The above answer refers to C++, but the same is true for C.
From "7.19.6.2 The fscanf function" in the "Final version of the C99 standard with corrigenda TC1, TC2, and TC3 included, formatted as a draft" (link copied from http://en.wikipedia.org/wiki/C99):
a,e,f,g
Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected for the subject sequence of the strtod function. The corresponding argument shall be a pointer to floating.The conversion specifiers A, E, F, G, and X are also valid and behave the same as, respectively, a, e, f, g, and x.
So %f, %e, %g, %E, %G
all behave identically when scanning numbers, as you experienced.
f,e,g
all are for Floating point number
From the doc:-
A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod). Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
Also check this reference which says that it(f,e,g
) matches a floating-point number.
C displays both float and double variables to six decimal places. This does NOT refer to the precision (accuracy) of which the number is actually stored, only how many decimal places printf()
uses to display these variable types.
The following program illustrates how the different data types are declared and displayed:
#include <stdio.h>
main()
{
float a = 23.567;
double b = 11e+23;
printf("Float variable is %f\n", a);
printf("Double variable is %e\n", b);
}
OUTPUT
Float variable is 23.567000
Double variable is 11.000000e23
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With