Assinging values to type float

Roger House roger at everexn.uucp
Fri Aug 31 03:05:44 AEST 1990


In <90240.003415RHMCSUPV at MIAMIU.BITNET> RHMCSUPV at MIAMIU.BITNET (Douglas M. MacFarlane) writes:

>Excuse the novice question . . .
> 
>I'm new to C and liking it, but this puzzles me.  I'm using Microsoft
>C ver 5.1.   When I assign a value to a float variable, I get a data-
>conversion warning (with warning level 2 set).  Why?
> 
>For example:
> 
>float fValue ;     /* define fValue as a floating point variable  */
> 
>other lines
> 
>fValue = 35 ;     /* produces a Data Conversion warning at compile time */
> 
> 
>Whasss Happening ????

The type of the constant 35 is int.  Thus, in order to assign it to a float
variable, the constant must be converted to type float.  This is what the
warning is telling you.  Most likely the compiler converts 35 to float so
that there is no runtime overhead associated with the conversion.  It is not
clear that the warning in this case is of much value.  However, in the case
of fValue = i, where i is of type int, the warning is of use because it draws
your attention to the fact that a conversion will be done at runtime.  The
obvious way to get rid of the warning is to write fValue = 35.0.  However, if
I remember MSC 5.1 correctly, this will also cause a warning.  Reason:  The
constant 35.0 is of type double.  Thus, in order to assign it to a float,
there must be a conversion from type double to type float.  Is there any way
out?  Yes:  fValue = (float) 35.

						Roger House



More information about the Comp.lang.c mailing list