![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 4
![]() |
Retreiving a double from scanf() using "%f"
Here's the code:
int main(void)
{
double value;
printf("Enter a temperature in Farenheit: ");
scanf("%f", &value);
printf("%f", value);
return 0;
}Obviously this is part of a bigger program, but this really should work, although I can't figure out why it doesn't. I've tried entering numbers with decimal values, but the result is always 0.000000. It does seem to work if I change 'value' to float instead of double, but I don't want that. Any suggestions? I'm using the VC++ free tools from M$ if that makes a difference. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Try using %lf instead of %f
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 4
![]() |
It works. Hmm, why is it that the book I'm using makes no mention of the %lf property? It is from like 2001, but I didn't think it would be too outdated.
Thanks.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me... |
|
|
|
|
|
#4 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>why is it that the book I'm using makes no mention of the %lf property?
Probably because it doesn't cover scanf adequately. That suggests your book isn't of the reference category, but more of a tutorial that's cut down to be easier to follow. Books like that typically use float instead of double for some strange reason, and %f means "pointer to float" to scanf. On the other hand, if your book actually does use %f with double, you should probably toss the book and get a better one. If an author makes such ameteurish mistakes, it's unlikely that he can properly teach you C.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|