15

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

lee77
  • 1,493
  • 3
  • 10
  • 14
  • 3
    You might want to read e.g. [this reference](http://en.cppreference.com/w/c/io/fscanf). – Some programmer dude Nov 10 '13 at 20:07
  • Thanks - this reference is on C++, that's why I didn't fully trust it before... Edited my question now, adding "C" – lee77 Nov 10 '13 at 20:12
  • 2
    The POSIX page for [`fscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html) reflects the C standard and adds some (carefully marked) extensions. It says that conversion specifiers `a`, `e`, `f`, `g`, `A`, `E`, `F`, `G` all handle floating point numbers in any of the formats. Exponents are always accepted (but never required). Decimal points are accepted but not required. – Jonathan Leffler Nov 10 '13 at 20:56

6 Answers6

11

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
2

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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

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
whoan
  • 8,143
  • 4
  • 39
  • 48
1

%f prints the number as a decimal floating point number, e.g. 214.52. %e prints the number in scientific notation, e.g. 214.52e+2. %g prints the number in the shortest among two, e.g. In the above two examples, if I want to print the number through %g then it would be 214.52 but not the 2.145e+2 as it is longer.

#include<stdio.h>
int main(int argc, char *argv[]) {
    float f=214.52;   
    printf("%f\n",f); // 214.520004
    printf("%e\n",f); // 2.145200e+02
    printf("%g\n",f); // 214.52
}
hmofrad
  • 1,784
  • 2
  • 22
  • 28
0

base on @Sameer Sharma answer

$ cat test.c 
#include<stdio.h>
void main()
{

float f=12.5123;

    printf("%f\n",f);
    printf("%e\n",f);
    printf("%g\n",f);
}

$ gcc test.c
$ ./a.out
12.512300
1.251230e+01
12.5123
hukeping
  • 665
  • 7
  • 12
-1
%i=defaults for decimals
%u=gives the memory address of the variable
%p=gives the value of pointer
%e=gives the scientific notation
%g=handles large floating numbers
mohit
  • 11