Parenthesis issue?

I've run across a strange issue that's easier to demonstrate than to write out.
Its a macro that converts Celsius to Fahrenheit.

#define CtoF(c) ((c * (1.8)) + 32) // This works as expected

// #define CtoF(c) ((c * (9/5)) + 32) This version fails. Why?
// #define CtoF(c) ((c * 9/5) + 32) // this works

When I try to convert 20C (68F) this is what the result is.

First version: 68
Second version: 52 ?! (almost like c+32 and the rest is ignored).

Why is the formula failing when 9/5 = 1.8 anyway?

integer division of 9/5 is 1, not 1.8.

you could try 9./5

Ah ok I added a typecast and it works. Thanks for the help.

#define CtoF(c) ((c * ((float)9/5) + 32)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.