C: converting Farenheit to Celsius

前端 未结 3 618
我在风中等你
我在风中等你 2020-12-02 03:10
int main (void)
{
    int fahrenheit; // fahrenheit stands for fahrenheit
    double c; // c stands for celsius

    printf(\"Enter your fahrenheit, we\'ll covnvert          


        
3条回答
  •  旧时难觅i
    2020-12-02 03:38

    The scanf call uses the wrong format string. You are reading an int so you need it to be:

    scanf("%d", &fahrenheit);
    

    The expression 5/9 is evaluated using integer division. In fact the compiler can work it out at compile time. That expression evaluates to 0.

    You need to perform floating point division. For instance:

    5.0/9
    

    Or:

    5/9.0
    

    Or

    5.0/9.0
    

    You just need at least one operand to be a floating point value.

    Putting this into your expression, you can write:

    c = 5.0/9.0 * (fahrenheit - 32);
    

    and obtain the answer that you expect.


    Your printf statement is wrong too. You should enable warnings and let the compiler tell you that. You meant to write:

    printf("Here is your %f in celsius!.\n", c);
    

提交回复
热议问题