scanf to handle double

Discussion in 'C Programming' started by Red Dragon, Nov 3, 2005.

  1. Red Dragon

    Red Dragon Guest

    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
    */
     
    Red Dragon, Nov 3, 2005
    #1
    1. Advertising

  2. Red Dragon

    Mike Wahler Guest

    "Red Dragon" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Nov 3, 2005
    #2
    1. Advertising

  3. Red Dragon

    Red Dragon Guest

    > scanf("%lf", &a);
    >
    > -Mike


    Thanks Mike,
    With your help I learnt something today.
    Regards,
    Khoon.
     
    Red Dragon, Nov 3, 2005
    #3
  4. Red Dragon

    Old Wolf Guest

    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.
     
    Old Wolf, Nov 3, 2005
    #4
  5. Red Dragon

    Mike Wahler Guest

    "Old Wolf" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Nov 3, 2005
    #5
  6. Red Dragon

    Michael Mair Guest

    Mike Wahler wrote:
    > "Old Wolf" <> wrote in message
    > news:...
    >
    >>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.
     
    Michael Mair, Nov 3, 2005
    #6
  7. Red Dragon

    Red Dragon Guest

    >
    > 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.
     
    Red Dragon, Nov 3, 2005
    #7
  8. Red Dragon

    Michael Mair Guest

    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.
     
    Michael Mair, Nov 3, 2005
    #8
  9. 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
     
    Peter Nilsson, Nov 3, 2005
    #9
  10. Red Dragon

    Red Dragon Guest

    > 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.
     
    Red Dragon, Nov 3, 2005
    #10
  11. Red Dragon

    Red Dragon Guest

    > 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.
     
    Red Dragon, Nov 3, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,568
    Victor Bazarov
    Feb 17, 2005
  2. Frank Chow

    Problem: scanf used for double

    Frank Chow, Oct 16, 2003, in forum: C Programming
    Replies:
    16
    Views:
    631
    Irrwahn Grausewitz
    Oct 17, 2003
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,452
    those who know me have no need of my name
    Apr 3, 2006
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    690
    Richard Bos
    May 2, 2006
  5. amarapreet

    scanf double

    amarapreet, Aug 7, 2008, in forum: C Programming
    Replies:
    3
    Views:
    447
    Keith Thompson
    Aug 8, 2008
Loading...

Share This Page