What's wrong with this code?

This is a discussion on What's wrong with this code? within the C Programming forums, part of the General Programming Boards category; I'm a beginner. This is an example code from my book and it keeps producing a warning (warning C4244: '=' ...

The preprocessor replaces text matching DENSITY with the text 62.4. The compiler then analyzes this. The line read by the compiler is thus:

Code:

volume = weight / 62.4;

Now, in an expression, the type of the result is the type of the longest, floatiest operand. The type of weight is float, but the type of a floating-point literal is double. double is floatier(*) than float, so the result of the expression weight / 62.4 is double. You then attempt to assign a temporary value with the type double to a variable of type float. This is an assignment of a wider type to a narrower type and requires a diagnostic.

The solution is to either cast the result of the expression to (float), or cause the floating-point literal to be float by appending F or f: