(novice) I'm Having trouble scanf'ing a double ?
Author Message
(novice) I'm Having trouble scanf'ing a double ?

Quote:

>Just when I thought I understood C...
>I am having trouble using doubles. My code looks something like this:

>double mean;
>....
>scanf ("%f", &mean);
>....

>When I put my de{*filter*} on the line just after the scanf (which should have just
>read the number 1) mean is usually something ridiculously small (like 3.3123
>E-38 or somesuch). If I declare mean to be a float, however, it seems to
>work. But, I think I will need the extra precision.

>Are there some special contortions I need to go through to read this in ?
>I can't find anything about scanning doubles in any of my C books.

>Thanks, I was embarassed to ask,
>Terry

You need format %lf for doubles in scanf, instead of %f.

Sun, 11 Feb 1996 06:09:18 GMT
(novice) I'm Having trouble scanf'ing a double ?

Quote:

>Just when I thought I understood C...
>I am having trouble using doubles. My code looks something like this:

>double mean;
>...
>scanf ("%f", &mean);
>...

>When I put my de{*filter*} on the line just after the scanf (which should have just
>read the number 1) mean is usually something ridiculously small (like 3.3123
>E-38 or somesuch). If I declare mean to be a float, however, it seems to
>work. But, I think I will need the extra precision.

>Are there some special contortions I need to go through to read this in ?
>I can't find anything about scanning doubles in any of my C books.

>Thanks, I was embarassed to ask,
>Terry

Dear Terry,
Y'all didn't say which compiler you were using, but I'm assuming
that it behaves like the three I've used: doubles are bigger than floats.
That being the case, you have to tell scanf how big a buffer is available
for the read.  So the long-float format %lf might do it.
Now _my_ question: what is the format for reading in a tenbyte (long
double) in Turbo C++?  Seems like a shame not to play with variables
which can store such grandmungous values :)....

Sun, 11 Feb 1996 03:37:32 GMT
(novice) I'm Having trouble scanf'ing a double ?

Quote:
>Now _my_ question: what is the format for reading in a tenbyte (long
>double) in Turbo C++?  Seems like a shame not to play with variables
>which can store such grandmungous values :)....

It's a shame to complain before RTFM :-)
According to the standard, it's "%Lf".

Dan
--
Dan Pop
Tel:   +41.22.767.2335

Mail:  CERN - PPE, Bat. 21 1-023, CH-1211 Geneve 23, Switzerland

Sun, 11 Feb 1996 20:42:47 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages