Thread: scanf not functioning? impossible value received

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    scanf not functioning? impossible value received

    I'm a newbie and just starting to learn C

    I tried to use scanf for the first time:

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    
    int main()
    {
     double rate;
     
     printf("how high is the rate : ");
     scanf("%f", &rate);
    
     printf("rate is %f\n", rate);
    
     return 0;
    }
    I enter => rate= .04
    I got => rate is -9255596313 etc.... instead of 0.04

    What is wrong? FYI I'm using Visual C++ 2008 as compiler and the name of the program ends with .c

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The format specifier for scanf() to read a double is actually %lf, not %f (which is for float).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Okay, I changed %f to %lf

    I enter => rate= .04
    I got => rate is (blank)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    On a hunch: printf's format specifier should remain as %f. I did not tell you to change it, but maybe you did and Microsoft's compilers' poor support for C99 is in effect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    THANK YOU!!

    I did change that =)

    Now it works well. May I ask why it should stay %f at printf while it's %lf at scanf?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf received float var which is by default promoted to double, so float and double have the same format

    scanf receives float* which is different from double*

    so float* and double* need to have different format specifiers in scanf (the function needs to know how many bytes of memory are available for writing)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vsovereign
    May I ask why it should stay %f at printf while it's %lf at scanf?
    That is just the way it works: for double, %lf is for scanf and %f is for printf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Thank you for the help everyone.
    All is clear now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tuncated uint8_t
    By Coens in forum C Programming
    Replies: 14
    Last Post: 11-24-2008, 07:57 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. WSAGetLastError() is returning 0
    By 39ster in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-16-2008, 06:57 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

Tags for this Thread