weirdness of scanf

Discussion in 'C Programming' started by Kevin Zhou, May 16, 2004.

  1. Kevin Zhou

    Kevin Zhou Guest

    #include <stdio.h>

    main()
    {
    double a;
    scanf("%f", &a);
    printf("%f",a);
    }
    the output is always -1.998295 no matter what value i put in

    but

    #include <stdio.h>

    main()
    {
    int a;
    scanf("%d", &a);
    printf("%d",a);
    }

    works wine
     
    Kevin Zhou, May 16, 2004
    #1
    1. Advertising

  2. Kevin Zhou

    Kevin Zhou Guest

    Kevin Zhou wrote:
    > #include <stdio.h>
    >
    > main()
    > {
    > double a;
    > scanf("%f", &a);
    > printf("%f",a);
    > }
    > the output is always -1.998295 no matter what value i put in
    >
    > but
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > int a;
    > scanf("%d", &a);
    > printf("%d",a);
    > }
    >
    > works wine


    problem solved
    should have used scanf("%lf",&a)
    in K&R lf looks like 1f and it doesn't explain why
     
    Kevin Zhou, May 16, 2004
    #2
    1. Advertising

  3. Kevin Zhou

    Leor Zolman Guest

    On Sat, 15 May 2004 22:19:54 -0400, Kevin Zhou <> wrote:

    >Kevin Zhou wrote:
    >> #include <stdio.h>
    >>
    >> main()
    >> {
    >> double a;
    >> scanf("%f", &a);
    >> printf("%f",a);
    >> }
    >> the output is always -1.998295 no matter what value i put in
    >>
    >> but
    >>
    >> #include <stdio.h>
    >>
    >> main()
    >> {
    >> int a;
    >> scanf("%d", &a);
    >> printf("%d",a);
    >> }
    >>
    >> works wine

    >
    >problem solved
    >should have used scanf("%lf",&a)
    >in K&R lf looks like 1f and it doesn't explain why


    So, do you understand why, now? There /must/ be two different format
    conversions for float and double, because the types have different storage
    requirements (at least on most systems), and scanf has to know exactly how
    wide the object is it's modifying.

    The reason this may come as a surprise to beginners is that the
    printf-family doesn't distinguish those types. But that's because floats
    get promoted to doubles automatically during the call, so the
    printf-family functions still see doubles, even when the caller writes
    arguments of type float in the argument list.
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, May 16, 2004
    #3
  4. Kevin Zhou wrote:
    > #include <stdio.h>
    >
    > main()

    ^
    The return type of main is int; you should say so. When you get a C99
    compiler, you will be *required* to say so.

    > {
    > double a;
    > scanf("%f", &a);

    ^^
    The scanf specifier for double is "%lf".

    > printf("%f",a);

    ^^^
    Without an end-of-line character terminating the last line of output,
    the behavior of your program is not predictable across platforms.
    > }
    > the output is always -1.998295 no matter what value i put in


    Try this version instead:

    #include <stdio.h>

    int main(void)
    {
    double a;
    scanf("%lf", &a);
    printf("%f\n", a);
    return 0;
    }
     
    Martin Ambuhl, May 16, 2004
    #4
  5. Kevin Zhou

    Karthik Guest

    Kevin Zhou wrote:

    > #include <stdio.h>
    >
    > main()
    > {
    > double a;
    > scanf("%f", &a);


    scanf("%lf", &a);

    > printf("%f",a);


    scanf("%lf", &a);

    should fix the bug.

    > }
    > the output is always -1.998295 no matter what value i put in
    >
    > but
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > int a;
    > scanf("%d", &a);
    > printf("%d",a);
    > }
    >
    > works wine



    --
    Karthik.
    Humans please 'removeme_' for my real email.
     
    Karthik, May 16, 2004
    #5
  6. Kevin Zhou

    Dan Pop Guest

    In <> Leor Zolman <> writes:

    >The reason this may come as a surprise to beginners is that the
    >printf-family doesn't distinguish those types. But that's because floats
    >get promoted to doubles automatically during the call, so the
    >printf-family functions still see doubles, even when the caller writes
    >arguments of type float in the argument list.


    Actually, most beginners who get it wrong expect printf to use %lf for
    doubles. The mistake was popular enough for C99 to actually bless it and
    %lf in printf is no longer undefined behaviour, as it was in C89.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 17, 2004
    #6
  7. Kevin Zhou

    Leor Zolman Guest

    On 17 May 2004 15:46:26 GMT, (Dan Pop) wrote:

    >In <> Leor Zolman <> writes:
    >
    >>The reason this may come as a surprise to beginners is that the
    >>printf-family doesn't distinguish those types. But that's because floats
    >>get promoted to doubles automatically during the call, so the
    >>printf-family functions still see doubles, even when the caller writes
    >>arguments of type float in the argument list.

    >
    >Actually, most beginners who get it wrong expect printf to use %lf for
    >doubles. The mistake was popular enough for C99 to actually bless it and
    >%lf in printf is no longer undefined behaviour, as it was in C89.


    Cool. I didn't know that. In beginning C classes I teach, though, despite
    careful explanation of the conversions for scanf, there's typically someone
    who'll use %f for doubles. I think it is a natural mistake, compounded by
    the fact that trying to remember the conversions mnemonically by data type
    is a non-starter [you mean %d isn't for doubles? :) ]
    -leor

    >
    >Dan


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, May 17, 2004
    #7
    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. Wayne Myers

    POK Flag Weirdness wth binary files

    Wayne Myers, May 26, 2004, in forum: Perl
    Replies:
    1
    Views:
    500
    ! aaa
    May 27, 2004
  2. Replies:
    4
    Views:
    733
    J├╝rgen Exner
    Dec 7, 2004
  3. steve
    Replies:
    4
    Views:
    558
    Brian van den Broek
    Mar 13, 2005
  4. =?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,524
    those who know me have no need of my name
    Apr 3, 2006
  5. =?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:
    709
    Richard Bos
    May 2, 2006
Loading...

Share This Page