By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

scanf double

P: n/a
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf
format specifier is this right if so how to read in a double using scanf
thanks
Aug 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
hi sorry this is c90 options recommended in this forum -W -Wall -ansi
-pedantic thanks

amarapreet wrote:
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf
format specifier is this right if so how to read in a double using scanf
thanks
Aug 7 '08 #2

P: n/a
amarapreet said:
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf
format specifier is this right if so how to read in a double using scanf
Whilst compilers are given licence to produce any diagnostic messages they
like, from a Quality-of-Implementation perspective it would be best if
they refrained from giving misleading information. %lf is the appropriate
format specifier for reading a value into a double via *scanf, and I can
confirm that gcc 2.95.3 gets this right. I would be very surprised to
learn that gcc 3.4 gets it wrong. Can anyone confirm this report?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 7 '08 #3

P: n/a
On Aug 7, 11:56*pm, amarapreet <a...@localhost.localdomainwrote:
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf
format specifier is this right if so how to read in a double using scanf
thanks
Can you post an equivalent program that exhibits that compile time
diagnostic to be generated?
Could you also post the command line you use to invoke the compiler?
Thanks.
Aug 7 '08 #4

P: n/a
Pietro Cerutti wrote:
return (EXIT_SUCCESSc);
yep, now you tell me to post actual code....

--
Pietro Cerutti
Aug 7 '08 #5

P: n/a
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:le******************************@bt.com...
amarapreet said:
>hi gcc 3.4 says in a warning that %lf is not recognized as a scanf
format specifier is this right if so how to read in a double using scanf

Whilst compilers are given licence to produce any diagnostic messages they
like, from a Quality-of-Implementation perspective it would be best if
they refrained from giving misleading information. %lf is the appropriate
format specifier for reading a value into a double via *scanf, and I can
confirm that gcc 2.95.3 gets this right. I would be very surprised to
learn that gcc 3.4 gets it wrong. Can anyone confirm this report?
No problem with 3.2:
dcorbit@DCORBIT64 /c/tmp
$ cat foo.c
#include <stdio.h>
#include <math.h>
#include <float.h>
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502884197169399375 1
#endif
#define deg2radian(x) ((M_PI/180) * (x))
int main(void)
{
double theta,
phi;
double thetahat[3],
phihat[3];
double r;
double rhat[3];
int converted;

oops:
printf("Enter theta, phi, r\n");
converted = scanf("%lf %lf %lf", &theta, &phi, &r);
if (converted != 3) goto oops;

theta = deg2radian(theta);
phi = deg2radian(phi);
thetahat[0] = -sin(theta);
thetahat[1] = cos(theta);
thetahat[2] = 0;
phihat[0] = cos(theta) * cos(phi);
phihat[1] = sin(theta) * cos(phi);
phihat[2] = -sin(phi);
rhat[0] = thetahat[1] * phihat[2] - thetahat[2] * phihat[1];
rhat[1] = thetahat[2] * phihat[0] - thetahat[0] * phihat[2];
rhat[2] = thetahat[0] * phihat[1] - thetahat[1] * phihat[0];

printf("%*.*g %*.*g %*.*g\n ", DBL_DIG + 3, DBL_DIG, rhat[0], DBL_DIG +
3, DBL_DIG, rhat[1], DBL_DIG + 3, DBL_DIG, rhat[2]);
return 0;
}

standard cat took 0.000000 seconds

dcorbit@DCORBIT64 /c/tmp
$ gcc --version
gcc.exe (GCC) 3.2 (mingw special 20020817-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
dcorbit@DCORBIT64 /c/tmp
$ gcc -W -Wall -ansi -pedantic foo.c -lm
No problem with 3.3:

% cat foo.c
#include <stdio.h>
#include <math.h>
#include <float.h>
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502884197169399375 1
#endif
#define deg2radian(x) ((M_PI/180) * (x))
int main(void)
{
double theta,
phi;
double thetahat[3],
phihat[3];
double r;
double rhat[3];
int converted;

oops:
printf("Enter theta, phi, r\n");
converted = scanf("%lf %lf %lf", &theta, &phi, &r);
if (converted != 3) goto oops;

theta = deg2radian(theta);
phi = deg2radian(phi);
thetahat[0] = -sin(theta);
thetahat[1] = cos(theta);
thetahat[2] = 0;
phihat[0] = cos(theta) * cos(phi);
phihat[1] = sin(theta) * cos(phi);
phihat[2] = -sin(phi);
rhat[0] = thetahat[1] * phihat[2] - thetahat[2] * phihat[1];
rhat[1] = thetahat[2] * phihat[0] - thetahat[0] * phihat[2];
rhat[2] = thetahat[0] * phihat[1] - thetahat[1] * phihat[0];

printf("%*.*g %*.*g %*.*g\n ", DBL_DIG + 3, DBL_DIG, rhat[0], DBL_DIG +
3, DB

return 0;
}

% gcc -W -Wall -ansi -pedantic foo.c -lm
% gcc --version
gcc (GCC) 3.3
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%

Unfortunately, I don't have 3.4 installed on anything right now.

I guess that if we see his code, the actual error will become obvious.

** Posted from http://www.teranews.com **
Aug 7 '08 #6

P: n/a
amarapreet <am**@localhost.localdomainwrites:
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf
format specifier is this right if so how to read in a double using
scanf thanks
"%lf" is the correct format specifier to read a double using scanf in
both C90 and C99.

You haven't shown us either your code or the warning. If you had
posted the exact code that you fed to the compiler (copy-and-paste a
complete program; don't try to re-type it) *and* the exact diagnostics
the compiler gave you, then we could probably tell you what the
problem is. Given the lack of information you've shared with us,
though, we can only guess.

Here's my guess. You wrote something like this:

double d;
scanf("%lf", d);

and the warning was something like this:

c.c:5: warning: format '%lf' expects type 'double *', but argument 2 has type 'double'

scanf needs the address of the object you're reading, not the value of
the object:

double d;
scanf("%lf", &d);

Don't blame me if I've guessed wrong. Show us the code and the
warning.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 8 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.