>> >> 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. No, you misunderstood C. C, despite being lower level than (say) Java, does not view variables as typeless bit patterns. It views them as integers, real numbers, etc. So if you perform float real_number = 0.5; int integer = real_number; The value of "integer" will be 0; if C were to actually interpret the bit pattern of real_number as an integer, you would get 1056964608 (0x3f000000) - at least on my machine. That is not what C does, though.
The real problem is when you type float real_number = 4e10; int integer = real_number; If your integer can only hold values up to 2^31 - 1 , the behavior of the above code is undefined. In a language like Python, everything either behaves as you intended, of throws an exception. This is why I say "In C, you must completely understand the behavior of every statement or function, and you *must* handle the possibility of errors". -- Software is like sex: it is better when it is free - Linus Torvalds