7

I need some help with a program for converting Fahrenheit to Celsius in C. My code looks like this

#include <stdio.h>
int main(void)
{
    int fahrenheit;
    double celsius;

    printf("Enter the temperature in degrees fahrenheit:\n\n\n\n");
    scanf("%d", &fahrenheit);
    celsius = (5 / 9) * (fahrenheit - 32);
    printf("The converted temperature is %lf\n", celsius);

    return 0;
}

Every time I execute it it the result is 0.000000. I know I'm missing something but can't figure out what.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
James
  • 89
  • 1
  • 1
  • 4
  • 8
    You are working with integers, you need to work with floats or doubles. – Joe Feb 03 '11 at 19:04
  • 4
    Side note, compilers usually consider 5.0 a double. You have to say 5.0f to do float value calcs. Seems more relevant to mobile – Stephen J Mar 29 '12 at 22:51

6 Answers6

24

5/9 will result in integer division, which will = 0

Try 5.0/9.0 instead.

nybbler
  • 4,793
  • 28
  • 23
  • 4
    technically speaking, 5/9.0 suffices – Foo Bah Feb 03 '11 at 19:06
  • @FooBah: You are, of course, correct. I tend to apply it to both for cases like this for my own good practice. – nybbler Feb 03 '11 at 19:12
  • 6
    @Foo Bah - Perhaps, but it is a good policy to avoid as many implicit conversions as possible. The less the compiler changes types for you, the less surprises you will have. – T.E.D. Feb 03 '11 at 19:16
  • @T.E.D. In that case, `fahrenheit` and `32` will have to cast to double as well ;-) In general, such expressions would have a ridiculous amount of casts if we were to avoid "implicit conversions" everywhere. IMHO, *understanding* is more important than *avoiding* especially in C, because implicit conversions, type promotions, and so on are a fact of life in C programming. – P.P Dec 25 '20 at 21:35
9

You problem is here :

celsius = (5/9) * (fahrenheit-32);

5/9 will always give you 0. Use (5.0/9.0) instead.

Incognito
  • 16,567
  • 9
  • 52
  • 74
2

try celsius = ((double)5/9) * (fahrenheit-32); Or you can use 5.0.

The fact is that "/" looks at the operand type. In case of int the result is also an int, so you have 0. When 5 is treated as double, then the division will be executed correctly.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

You need to use floating point arithmetic in order to perform these type of formulas with any accuracy. You can always convert the final result back to an integer, if needed.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

write 5/9.0 instead of 5/9 -- this forces double division

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • thanks for the help everyone, 5.0/9.0 did the trick, also switching to double worked as well. thanks again – James Feb 03 '11 at 19:10
0

When dealing with floats, it needs to be 5.0f / 9.0f.

When dealing with doubles, it needs to be 5.0 / 9.0.

When dealing with integers, remainders/fractions are always truncated. 5 / 9 results between 0 and 1, so it is truncated to just 0 every time. That multiplies the other side by zero and completely nullifies your answer every time.

TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58