Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf to handle double

Reply
Thread Tools

scanf to handle double

 
 
Red Dragon
Guest
Posts: n/a
 
      11-03-2005
Can someone please tell me how I should write scanf to handle double?
This program works fine for float a but not double a, as the print result
at end of program illustrates.
Thanks
Khoon

#include <stdio.h>

int main (void)
{
double a;
a =2.1111;

printf ("Enter a> ");
scanf ("%f", &a);
printf ("a=%4.4f\n",a);
return 0;
}

/*
Result:
Enter a> 555.55
a=2.1111
Press any key to continue
*/


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      11-03-2005

"Red Dragon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can someone please tell me how I should write scanf to handle double?
> This program works fine for float a but not double a, as the print
> result at end of program illustrates.
> Thanks
> Khoon
>
> #include <stdio.h>
>
> int main (void)
> {
> double a;
> a =2.1111;
>
> printf ("Enter a> ");
> scanf ("%f", &a);


scanf("%lf", &a);

-Mike


 
Reply With Quote
 
 
 
 
Red Dragon
Guest
Posts: n/a
 
      11-03-2005
> scanf("%lf", &a);
>
> -Mike


Thanks Mike,
With your help I learnt something today.
Regards,
Khoon.


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      11-03-2005
Red Dragon wrote:
> Mike Wahler wrote:
>
>> scanf("%lf", &a);

>
> With your help I learnt something today.


Note that printf still uses "%f" for doubles. Don't try using
"%lf" with printf as it may not work.

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-03-2005

"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Red Dragon wrote:
>> Mike Wahler wrote:
>>
>>> scanf("%lf", &a);

>>
>> With your help I learnt something today.

>
> Note that printf still uses "%f" for doubles. Don't try using
> "%lf" with printf as it may not work.


Yes, this trips up many folks. The lists of 'printf()'
and 'scanf()' format specifiers are very close, but not
exactly the same.

-Mike


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      11-03-2005
Mike Wahler wrote:
> "Old Wolf" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>Red Dragon wrote:
>>
>>>Mike Wahler wrote:
>>>
>>>
>>>>scanf("%lf", &a);
>>>
>>>With your help I learnt something today.

>>
>>Note that printf still uses "%f" for doubles. Don't try using
>>"%lf" with printf as it may not work.

>
> Yes, this trips up many folks. The lists of 'printf()'
> and 'scanf()' format specifiers are very close, but not
> exactly the same.


Note: C99 allows "%lf" for doubles.
As this does not concern many people, "%f" of course is the
correct thing.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Red Dragon
Guest
Posts: n/a
 
      11-03-2005
>
> Note that printf still uses "%f" for doubles. Don't try using
> "%lf" with printf as it may not work.


Yes. I noticed that. I wonder why the creater of C cannot do the same
for scanf as he has done for printf.
If he can do away with l in printf why cant he do so in scanf?
It would have made life easier for all of us, especially for myself. I got
tripped up earlier.
Thanks
Khoon.



 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      11-03-2005
Red Dragon wrote:
>>Note that printf still uses "%f" for doubles. Don't try using
>>"%lf" with printf as it may not work.

>
> Yes. I noticed that. I wonder why the creater of C cannot do the same
> for scanf as he has done for printf.
> If he can do away with l in printf why cant he do so in scanf?
> It would have made life easier for all of us, especially for myself. I got
> tripped up earlier.


The answer is clearer if you look at what happens:
For scanf(), one passes the address of an object, telling only
via the format specifier plus length modifier what kind of object
one is passing. If float and double have different sizes (or
different representations), then one _cannot_ use "%f" for
both -- if we assume different sizes, then we would only change
the first sizeof (float) bytes of the double object which almost
certainly does not yield the same value. Moreover, this may yield
an invalid double representation.
The other way round: For functions with variable argument lists,
float values are passed as double values. Thus, "%lf" would have
been better than "%f" for printf() but convenience won.

If you want to know more, I recommend looking at C's historical
development.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      11-03-2005
Red Dragon wrote:
> > Note that printf still uses "%f" for doubles. Don't try using
> > "%lf" with printf as it may not work.

>
> Yes. I noticed that. I wonder why the creater of C cannot do the same
> for scanf as he has done for printf.
> If he can do away with l in printf why cant he do so in scanf?


It's because the argument promotions which apply to printf are not
relevant to scanf. [Printf takes floats and converts them to doubles,
but
_pointers_ to floats are not converted to pointers to doubles in scanf
calls.]

> It would have made life easier for all of us, especially for myself.


Opening and reading a decent C book/tutorial would make life easier for
you
too. BTW, it's never too late for that.

--
Peter

 
Reply With Quote
 
Red Dragon
Guest
Posts: n/a
 
      11-03-2005
> If you want to know more, I recommend looking at C's historical
> development.


>
> Cheers
> Michael
> --

Good suggestion. I have decided to read up on this.
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
Very heavy reading indeed!
Thanks
Khoon.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scanf double amarapreet C Programming 3 08-08-2008 12:17 AM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Problem: scanf used for double Frank Chow C Programming 16 10-17-2003 05:52 PM



Advertisments