difference between statement

Discussion in 'C Programming' started by ashu, May 3, 2012.

  1. ashu

    ashu Guest

    Dear Sir ,
    i am writing a simple C programme but i am facing a problem kindly
    sort it out for me

    #include<stdio.h>
    #include<conio.h>

    main()
    {
    float fahr,celsius;

    celsius= -17;
    fahr=celsius * (9/5) + 32;
    printf("%3.1f\n",fahr);

    getch();
    }
    Result is 15.0
    but when i write: fahr=celsius * 9/5 + 32;
    result is : 1.4
    i can`t figure it out why 1st statement is not working.am i doing
    something wrong.i am using dev C++ compiler .
    ashu, May 3, 2012
    #1
    1. Advertising

  2. ashu

    John Gordon Guest

    In <> ashu <> writes:

    > Dear Sir ,
    > i am writing a simple C programme but i am facing a problem kindly
    > sort it out for me


    > #include<stdio.h>
    > #include<conio.h>


    > main()
    > {
    > float fahr,celsius;


    > celsius= -17;
    > fahr=celsius * (9/5) + 32;
    > printf("%3.1f\n",fahr);


    > getch();
    > }
    > Result is 15.0
    > but when i write: fahr=celsius * 9/5 + 32;
    > result is : 1.4
    > i can`t figure it out why 1st statement is not working.am i doing
    > something wrong.i am using dev C++ compiler .


    This expression:

    (9/5)

    is composed entirely of integers, therefore it is computed using integer
    math, which discards the fractional part and evaluates to 1.

    So your program is really doing this calculation:

    fahr = -17 * 1 + 32

    Which evaluates to 15.

    To fix this behavior, get in the habit of adding .0 after any number
    that you want to be treated as a float, like so:

    fahr = celsius * (9.0/5.0) + 32.0;

    This will give the correct results.

    (Your second example works because without the parentheses, the first
    operation is to multiply celsius by 9. One of those numbers is a float,
    so the calculation is done as a float. Then that result is divided by
    five, and again one of the numbers is a float, so the calculation is done
    as a float.)

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, May 3, 2012
    #2
    1. Advertising

  3. On May 3, 9:14 pm, ashu <> wrote:
    >       fahr=celsius * (9/5) + 32;


    (9/5) is 1

    Write something else, e.g. (9.0/5) or even (1.8)
    if you want 1.8 instead of 1.

    > but when i write: fahr=celsius * 9/5 + 32;
    > result is : 1.4


    That statement happens to lead to a chain of
    promotions that work as you intend.
    Better, however, is to avoid confusion or
    incorrectness by avoiding integer when you
    want floats. Thus write
    fahr=celsius * 9./5. + 32.;
    and don't worry about promotions.

    > i am using dev C++ compiler


    Oh, C++. That's that language two doors down the hall?
    The language that magically handles typing for you?

    That you have the above confusion and had to come
    to the C forum (not a C++ forum) to get it corrected
    tells us something about C++. :)

    James
    James Dow Allen, May 3, 2012
    #3
  4. ashu

    Fred K Guest

    On Thursday, May 3, 2012 7:14:10 AM UTC-7, ashu wrote:
    > Dear Sir ,
    > i am writing a simple C programme but i am facing a problem kindly
    > sort it out for me
    >
    > #include<stdio.h>
    > #include<conio.h>
    >
    > main()
    > {
    > float fahr,celsius;
    >
    > celsius= -17;
    > fahr=celsius * (9/5) + 32;
    > printf("%3.1f\n",fahr);
    >
    > getch();
    > }
    > Result is 15.0
    > but when i write: fahr=celsius * 9/5 + 32;
    > result is : 1.4
    > i can`t figure it out why 1st statement is not working.am i doing
    > something wrong.i am using dev C++ compiler .


    (9/5) equals 1 so celsius * (9/5) equals celsius
    Fred K, May 3, 2012
    #4
  5. ashu

    BartC Guest

    "ashu" <> wrote in message
    news:...

    > #include<stdio.h>
    > #include<conio.h>
    >
    > main()
    > {
    > float fahr,celsius;
    >
    > celsius= -17;
    > fahr=celsius * (9/5) + 32;
    > printf("%3.1f\n",fahr);
    >
    > getch();
    > }
    > Result is 15.0
    > but when i write: fahr=celsius * 9/5 + 32;
    > result is : 1.4


    Using floating point constants instead, such as 9.0/5.0, otherwise integer
    arithmetic can sometimes give these funny results (9/5 is 1 according to C).

    --
    Bartc
    BartC, May 3, 2012
    #5
  6. ashu

    ashu Guest

    Thankx for all help
    ashu, May 4, 2012
    #6
    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. jakk
    Replies:
    4
    Views:
    12,039
  2. Ken Philips
    Replies:
    4
    Views:
    48,888
    venkat.sadasivam
    Nov 5, 2008
  3. arnuld
    Replies:
    4
    Views:
    9,007
    osmium
    Jun 26, 2007
  4. Juha Nieminen
    Replies:
    5
    Views:
    716
    James Kanze
    Jun 26, 2009
  5. ashu
    Replies:
    20
    Views:
    713
    Tim Rentsch
    May 14, 2012
Loading...

Share This Page