>> 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".
>
> The line:
> int integer = real_number;
> will produce a warning. (or an error if you are smart enough to
> compile with -Werror)
It seems you did not get the point. To attribute a floating point
number to an integer variable is perfectly valid, depending on the
specific program.  The compiler normally does not even warn about
this, as this is perfectly valid (from my testing, the compiler only
warns if you are using gcc 4.3, and specify -Wconversion, an option
that is not included in -Wall and not even in -Wextra).

So erase this *wrong idea* that attributing floating-point value to an
integer variable is invalid or even just unwise. There is nothing
generally wrong with it.

The point is: in certain situations, the behavior is well-defined and
unsurprising. What happens, though, if (for example) the value of the
floating-point variable is too big for the int?
In a forgiving language, you would either have a sensible behavior
(such as the int receiving a INT_MAX value) or an error. In C the
behavior is *undefined*.

Got the point? In C, you *must* know what you are doing, and *must*
handle the possibility of errors. If not, your program is not even
garanteed to crash; it can, after an error, go on working
(erratically), possibly damaging user data or yielding subtly wrong
results without any warning.

-- 
Software is like sex: it is better when it is free - Linus Torvalds

Reply via email to