Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 30th, 2005, 1:52 AM   #1
bivhitscar
Hobbyist Programmer
 
bivhitscar's Avatar
 
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 4 bivhitscar is on a distinguished road
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.
bivhitscar is offline   Reply With Quote
Old Oct 30th, 2005, 2:12 AM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Try using %lf instead of %f
andro is offline   Reply With Quote
Old Oct 30th, 2005, 2:17 AM   #3
bivhitscar
Hobbyist Programmer
 
bivhitscar's Avatar
 
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 4 bivhitscar is on a distinguished road
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...
bivhitscar is offline   Reply With Quote
Old Oct 30th, 2005, 8:50 AM   #4
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC