On Wednesday 29 October 2008 00:55:42 Jorge Peixoto de Morais Neto wrote:
> >> I mean to really know C,
> >> that is, read a rigorous book such as "C: A Reference Manual" and be
> >> able to write portable programs with well-defined behavior. Speaking
> >> of well-defined behavior, do you know what happens when you cast a
> >> float to an int, and the float is too big to fit into the int?
> >
> > Did oyu try it yourself and see?
>
> The point is that the behavior in this situation is "undefined". It
> might do anything. Programming in C is different than programming in
> Python.

Most likely the compiler will try to treat the float as an int and use the 
first 4 bytes of the float, ignoring the rest.

This is insane though. I cannot think of any reason why one would ever want to 
treat the first 32 bits of a float as an int. It's not like you are casting a 
long to an int which can make sense - just discard the high bits.

I reckon the standard would say this is undefined. Most compiler would bomb 
out with a compile error but give you an obscure flag to proceed anyway. If 
you want to commit suicide, C is quite happy to pass you the pills as long s 
you ask nicely

-- 
alan dot mckinnon at gmail dot com

Reply via email to