Why do you need to put decimal point in the c language to get the result?

Asked 1 years ago, Updated 1 years ago, 342 views

Hello. I have a question about c language coding.

#include <stdio.h>
int main(void)
{ 
    double c, f;
    printf("Enter a Fahrenheit value: ");
    scanf_s("%lf", &f);

    c = 5.0 / 9.0 * (f - 32.0); // <<<---

    printf("C" value is %lf degrees\n", c);

    return 0; 
} 

When I first wrote the code, I wrote c = 5/9 * (f-32); and the result was

 Enter a Fahrenheit value: 100
 Celsius is 0.000000 degrees. 

That's what it says.

I wonder why I have to write down the decimal point to get the result.

c

2023-02-17 10:53

1 Answers

The compiler of language C recognizes 5 as int type and 5.0 as double type. Therefore, 5/9 is recognized as an int-type divided by int-type, and the division between int-type takes only the share, so the result of 5/9 becomes 0. Eventually, 5/9*(f-32) became 0*(f-32) and the result became 0.


2023-02-17 10:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.