Shaz
Shaz

Reputation: 273

Double conversion string

I have the following code:

FILE *fp;
double Data[9][7];
int i=0,k;                  
fp = fopen ( "dstest 2.mod", "r" ) ;
fscanf(fp, "%E %E %E %E %E %E %E*[ ]",
           &Data[i][0],
           &Data[i][1],
           &Data[i][2],
           &Data[i][3],
           &Data[i][4],
           &Data[i][5],
           &Data[i][6] );

But whenever I run it I get the following warnings:

Read.c:12: warning: format ‘%E’ expects type ‘float *’, but argument 3 has type ‘double *’

And it goes for all the arguments (Data[i][j]). I have no idea what's wrong. The problem goes away when I use float Data instead of double data. But that will not fit in my requirements.

Upvotes: 0

Views: 277

Answers (3)

Matteo Italia
Matteo Italia

Reputation: 126867

%E works with floats; for doubles you have to use %lE.

Sadly, inconsistency between the printf and the scanf here is not helping (printf does not support floats since all variadic arguments of type float are implicitly converted to double, but scanf does make a difference between the two FP types)

Upvotes: 0

thiton
thiton

Reputation: 36049

man scanf says that you need the l modifier character to read a double:

fscanf(fp, "%lE %lE...

and so on.

Upvotes: 2

K-ballo
K-ballo

Reputation: 81379

Use %lE to specify a double instead of a float.

Upvotes: 0

Related Questions