Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference in using %f, %e, %g, %E or %G with scanf?

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...

like image 665
lee77 Avatar asked Nov 10 '13 20:11

lee77


People also ask

How do printf format specifiers %E and %f differ in their?

%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.

What is the difference between %F and %1f in c?

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 .

Is %f for float or 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.

What does %G do in c?

%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.


3 Answers

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.

like image 80
Martin R Avatar answered Oct 19 '22 11:10

Martin R


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.

like image 26
Rahul Tripathi Avatar answered Oct 19 '22 11:10

Rahul Tripathi


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
like image 1
Keshav Bansal Avatar answered Oct 19 '22 12:10

Keshav Bansal