A Newbie Question

Discussion in 'C Programming' started by crackedh, Jan 17, 2008.

  1. crackedh

    crackedh Guest

    Hi,
    I'm a newbie to C, and to programming in general.

    My question is:
    -----------------------------------------------------------------------------------------------------------------------------------------------
    #include <stdio.h>
    main()
    {
    int fahr;
    for (fahr = 300; fahr >=0; fahr = fahr - 20)
    {
    printf("%3.0d %3.2f\n", fahr, (5.0/9.0) * (fahr -
    32));
    }
    }
    ------------------------------------------------------------------------------------------------------------------------------------------------
    #include <stdio.h>
    main()
    {
    int fahr;
    for (fahr = 300; fahr >=0; fahr = fahr - 20)
    {
    printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
    32));
    }
    }
    --------------------------------------------------------------------------------------------------------------------------------------------------

    Why is it that both these segments of code give very diffrent outputs.
     
    crackedh, Jan 17, 2008
    #1
    1. Advertising

  2. crackedh said:

    > Hi,
    > I'm a newbie to C, and to programming in general.
    >
    > My question is:
    >

    -----------------------------------------------------------------------------------------------------------------------------------------------
    > #include <stdio.h>
    > main()
    > {
    > int fahr;
    > for (fahr = 300; fahr >=0; fahr = fahr - 20)
    > {
    > printf("%3.0d %3.2f\n", fahr, (5.0/9.0) * (fahr -
    > 32));
    > }
    > }
    >

    ------------------------------------------------------------------------------------------------------------------------------------------------
    > #include <stdio.h>
    > main()
    > {
    > int fahr;
    > for (fahr = 300; fahr >=0; fahr = fahr - 20)
    > {
    > printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
    > 32));
    > }
    > }
    >

    --------------------------------------------------------------------------------------------------------------------------------------------------
    >
    > Why is it that both these segments of code give very diffrent outputs.


    The first program looks correct to me (although it is poor style not to
    return a value from main - if in doubt, return 0).

    The second program asks printf to display a double precision floating-point
    number, but provides fahr (an int, not a double) as the value to display.
    This breaks the rules of C, and any output you get is therefore arbitrary
    - indeed, the behaviour of the whole program becomes undefined.

    To display an int using printf, match it with %d. To display a double,
    match it with %f.

    The second format specifier in that line, %3.2f, is fine, because it's
    matched by a double. What's happening here is that fahr is an int and 32
    is an int, so fahr - 32 is an int, but then you multiply by a double (that
    is, 5.0/9.0), so the (fahr - 32) value is promoted to double for the
    purposes of the calculation, and the expression as a whole yields a
    double.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jan 17, 2008
    #2
    1. Advertising

  3. crackedh wrote:
    > Hi,
    > I'm a newbie to C, and to programming in general.
    >
    > My question is:
    > -----------------------------------------------------------------------------------------------------------------------------------------------
    > #include <stdio.h>
    > main()

    int main(void)

    > {
    > int fahr;
    > for (fahr = 300; fahr >=0; fahr = fahr - 20)
    > {
    > printf("%3.0d %3.2f\n", fahr, (5.0/9.0) * (fahr -
    > 32))
    > }

    return 0
    > }
    > ------------------------------------------------------------------------------------------------------------------------------------------------
    > #include <stdio.h>
    > main()

    int main(void)

    > {
    > int fahr;
    > for (fahr = 300; fahr >=0; fahr = fahr - 20)
    > {
    > printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
    > 32));

    fahr is int, %f expects double.

    > }

    return 0

    > }
    > --------------------------------------------------------------------------------------------------------------------------------------------------
    >
    > Why is it that both these segments of code give very diffrent outputs.

    Because you tell printf to expect a double (%f) but give an int.

    Bye, Jojo
     
    Joachim Schmitz, Jan 17, 2008
    #3
  4. crackedh

    crackedh Guest

    Thanks for the exdplanation.
    :)
     
    crackedh, Jan 17, 2008
    #4
  5. crackedh

    crackedh Guest

    Thanks
    :)
     
    crackedh, Jan 17, 2008
    #5
  6. crackedh

    Mark Bluemel Guest

    crackedh wrote:
    > #include <stdio.h>
    > main()
    > {
    > int fahr;
    > for (fahr = 300; fahr >=0; fahr = fahr - 20)
    > {
    > printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr - 32));


    You lied to printf - the "%3.0f" indicated that you would pass it a
    double value, and "fahr" is an int.
     
    Mark Bluemel, Jan 17, 2008
    #6
  7. crackedh wrote:
    [...]
    > int fahr;

    [...]
    > printf("%3.0f %3.2f\n", fahr, (5.0/9.0) * (fahr -
    > 32));


    fahr is an int. The specifier "%3.0f" tells printf that the
    corresponding argument (fahr) is a double (or a float whose value is
    passed as a double). You lied to printf. Why should you expect the
    output to make sense?
     
    Martin Ambuhl, Jan 17, 2008
    #7
  8. crackedh

    CBFalconer Guest

    crackedh wrote:
    >
    > Thanks for the exdplanation.
    > :)


    Don't answer anything without an explanatory quote. See below.

    --
    If you want to post a followup via groups.google.com, ensure
    you quote enough for the article to make sense. Google is only
    an interface to Usenet; it's not Usenet itself. Don't assume
    your readers can, or ever will, see any previous articles.
    More details at: <http://cfaj.freeshell.org/google/>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jan 17, 2008
    #8
    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. Jerry C.
    Replies:
    8
    Views:
    256
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    149
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    130
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    117
  5. Miki Tebeka
    Replies:
    0
    Views:
    88
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page