Thread: Whats this? I can't read doubles any more!

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Whats this? I can't read doubles any more!

    Code:
    #include <stdio.h>
    
    int main()
    {
        double b;
        
        scanf("%f", &b);
        printf("%f\n", b);
        
        system("pause");
        return 0;
    }
    I feel so weird, I have been programming for years and I'm having such a simple problem, when I input a number, the printed number is garbage.

    I tried this code in a different computer and in a diferent compiler and it still won't work! if I use a float instead of a double it does work WTF.

    Have a walked into the twilight zone or something?
    Why drink and drive when you can smoke and fly?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because %f is for reading floats and not doubles.
    Code:
    scanf( "%lf", &b );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    I can't beleive this, I have been programming for 4 years and I learn this until know? >_<

    In my defense I bearly ever use C >_>

    Thanks man, you have made it possible for me to sleep tonight.
    Why drink and drive when you can smoke and fly?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes it's quite annoying having the *printf and *scanf functions use similar, but different, format specifiers.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Another reason to use gcc with say -Wall, which will tell you when your *scanf and *printf conversion strings don't match the parameters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  2. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  3. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM