Problem: scanf used for double

Discussion in 'C Programming' started by Frank Chow, Oct 16, 2003.

  1. Frank Chow

    Frank Chow Guest

    Please see the following code:

    /* main.c */

    #include <stdio.h>

    int main()
    {
    double d;
    scanf("%f", &d);
    printf("%g\n", d);
    }

    The problem is, whatever I input to stdin (even an illegal data), the
    program just print a number that seems to be a data from "raw memory",
    ie uninitialized memory (for example: "5.35162e-315").

    And if I replace the statement "double d" with "double d = 3.14", then
    I'll always get the output "3.14" for whatever I input (even an
    illegal data).

    Then I may get the conclusion that the "scanf" statement never does
    its job. And I have tried this under 3 compilers and none of them give
    the right result.

    Please give me some explanation about this.

    Thanks.
     
    Frank Chow, Oct 16, 2003
    #1
    1. Advertising

  2. Frank Chow

    Eric Sosman Guest

    Frank Chow wrote:
    >
    > Please see the following code:
    >
    > /* main.c */
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > double d;
    > scanf("%f", &d);


    scanf("%lf", &d)

    See Question 12.13 in the comp.lang.c Frequently Asked
    Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html


    --
     
    Eric Sosman, Oct 16, 2003
    #2
    1. Advertising

  3. Frank Chow <> spoke thus:

    > #include <stdio.h>


    > int main()
    > {
    > double d;
    > scanf("%f", &d);
    > printf("%g\n", d);
    > }


    If you had your compiler warnings turned on, you probably would have gotten
    something like

    test.c:6: warning: float format, double arg (arg 2)

    The correct conversion specifier for doubles is %lf when using scanf. Also,
    should return a value from main, a la

    return( EXIT_SUCCESS );

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 16, 2003
    #3
  4. Christopher Benson-Manica <> wrote:

    <snip>
    > Also,
    >should return a value from main, a la
    >
    >return( EXIT_SUCCESS );


    after inclusion of stdlib.h, of course; alternatively

    return 0;

    is fine, too.
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 16, 2003
    #4
  5. Frank Chow

    Dan Pop Guest

    In <> (Frank Chow) writes:

    >Please see the following code:
    >
    >/* main.c */
    >
    >#include <stdio.h>
    >
    >int main()
    >{
    > double d;
    > scanf("%f", &d);
    > printf("%g\n", d);
    >}
    >
    >The problem is, whatever I input to stdin (even an illegal data), the
    >program just print a number that seems to be a data from "raw memory",
    >ie uninitialized memory (for example: "5.35162e-315").


    Where did you get the idea that %f is the right conversion specifier for
    a double? If you needed a float instead, what would you use?

    For obvious reasons, scanf is not a perfect mirror of printf.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 16, 2003
    #5
  6. Irrwahn Grausewitz <> spoke thus:

    > return 0;
    > is fine, too.


    I thought it was non-portable :(

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 16, 2003
    #6
  7. Christopher Benson-Manica <> wrote:

    >Irrwahn Grausewitz <> spoke thus:
    >
    >> return 0;
    >> is fine, too.

    >
    >I thought it was non-portable :(


    Nope, according to ISO/IEC 9899:1999 5.1.2.2.3 and 7.20.4.3#5
    it is perfectly portable.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 16, 2003
    #7
  8. "Frank Chow" <> wrote in message
    news:...
    > Please see the following code:
    >
    > /* main.c */
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > double d;
    > scanf("%f", &d);
    > printf("%g\n", d);
    > }
    >
    > The problem is, whatever I input to stdin (even an illegal data), the
    > program just print a number that seems to be a data from "raw memory",
    > ie uninitialized memory (for example: "5.35162e-315").


    Others have said that %f is wrong for double, which is true.

    Note that if scanf() doesn't find valid data to convert to a number it
    doesn't store anything. Also, it returns the number of valid conversions,
    which you can check.

    -- glen
     
    Glen Herrmannsfeldt, Oct 16, 2003
    #8
  9. Frank Chow wrote:
    > Please see the following code:
    >
    > /* main.c */
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > double d;
    > scanf("%f", &d);


    You mean scanf("%lf", &d);




    --
    Martin Ambuhl
     
    Martin Ambuhl, Oct 17, 2003
    #9
  10. Frank Chow

    Frank Chow Guest

    Thank you all. And indeed I should first read the C-faq to avoid such
    a naive mistake.
     
    Frank Chow, Oct 17, 2003
    #10
  11. Irrwahn Grausewitz <> spoke thus:

    > Nope, according to ISO/IEC 9899:1999 5.1.2.2.3 and 7.20.4.3#5
    > it is perfectly portable.


    <dumb>So if 0 is portable, why the EXIT_SUCCESS macro?</dumb>

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 17, 2003
    #11
  12. Christopher Benson-Manica <> wrote:

    >Irrwahn Grausewitz <> spoke thus:
    >
    >> Nope, according to ISO/IEC 9899:1999 5.1.2.2.3 and 7.20.4.3#5
    >> it is perfectly portable.

    >
    ><dumb>So if 0 is portable, why the EXIT_SUCCESS macro?</dumb>


    Because it's more descriptive and has a portable(!) and descriptive
    counter-part, EXIT_FAILURE.

    ISO/IEC 9899:1999 7.20.4.3
    5 [...] If the value of status is zero or EXIT_SUCCESS, an
    implementation-defined form of the status successful termination is
    returned. If the value of status is EXIT_FAILURE, an implementation-
    defined form of the status unsuccessful termination is returned.
    Otherwise the status returned is implementation-defined.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #12
  13. Irrwahn Grausewitz <> spoke thus:

    > Because it's more descriptive and has a portable(!) and descriptive
    > counter-part, EXIT_FAILURE.


    So it's kind of like the unary + thing, symmetry and all? I dunno, I always
    thought the l337-ness of C code was directly related to how obfuscated it
    looked ;)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 17, 2003
    #13
  14. Christopher Benson-Manica <> wrote:

    >Irrwahn Grausewitz <> spoke thus:
    >
    >> Because it's more descriptive and has a portable(!) and descriptive
    >> counter-part, EXIT_FAILURE.

    >
    >So it's kind of like the unary + thing, symmetry and all?


    Hm, well, yes and no. I can remember a discussion about this in c.l.c
    or c.s.c, where the claim was made that in the context of program exit
    codes 0 may describe a kind of successful termination different from
    what EXIT_SUCCESS results in. Whatever that means, practically.

    >I dunno, I always
    >thought the l337-ness of C code was directly related to how obfuscated it
    >looked ;)


    :)

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #14
  15. Frank Chow

    Dan Pop Guest

    In <bmoppn$p55$> Christopher Benson-Manica <> writes:

    >Irrwahn Grausewitz <> spoke thus:
    >
    >> Because it's more descriptive and has a portable(!) and descriptive
    >> counter-part, EXIT_FAILURE.

    >
    >So it's kind of like the unary + thing, symmetry and all?


    Exactly.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 17, 2003
    #15
  16. Frank Chow

    Dan Pop Guest

    In <> Irrwahn Grausewitz <> writes:

    >Christopher Benson-Manica <> wrote:
    >
    >>Irrwahn Grausewitz <> spoke thus:
    >>
    >>> Because it's more descriptive and has a portable(!) and descriptive
    >>> counter-part, EXIT_FAILURE.

    >>
    >>So it's kind of like the unary + thing, symmetry and all?

    >
    >Hm, well, yes and no. I can remember a discussion about this in c.l.c
    >or c.s.c, where the claim was made that in the context of program exit
    >codes 0 may describe a kind of successful termination different from
    >what EXIT_SUCCESS results in. Whatever that means, practically.


    Nothing at all, in the context of portable programming. Furthermore, the
    most natural interpretation of the standard is that both are mapped to
    the *same* form of successful termination.

    5 Finally, control is returned to the host environment. If the
    value of status is zero or EXIT_SUCCESS, an implementation-defined
    ^^
    form of the status successful termination is returned.

    The standard talks about a *single* "implementation-defined form of
    the status successful termination".

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 17, 2003
    #16
  17. (Dan Pop) wrote:

    >In <> Irrwahn Grausewitz <> writes:


    <snip>
    >>Hm, well, yes and no. I can remember a discussion about this in c.l.c
    >>or c.s.c, where the claim was made that in the context of program exit
    >>codes 0 may describe a kind of successful termination different from
    >>what EXIT_SUCCESS results in. Whatever that means, practically.

    >
    >Nothing at all, in the context of portable programming. Furthermore, the
    >most natural interpretation of the standard is that both are mapped to
    >the *same* form of successful termination.
    >
    >5 Finally, control is returned to the host environment. If the
    > value of status is zero or EXIT_SUCCESS, an implementation-defined
    > ^^
    > form of the status successful termination is returned.
    >
    >The standard talks about a *single* "implementation-defined form of
    >the status successful termination".


    It's the most natural interpretation, I agree. I just remembered the
    discussion, not the outcome...

    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #17
    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,646
    Victor Bazarov
    Feb 17, 2005
  2. Red Dragon

    scanf to handle double

    Red Dragon, Nov 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    747
    Red Dragon
    Nov 3, 2005
  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,517
    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:
    703
    Richard Bos
    May 2, 2006
  5. amarapreet

    scanf double

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

Share This Page