Shankar Unni <[EMAIL PROTECTED]> writes:
> Well, what is it supposed to do? Technically, according to the C standard,
> anyway, this operation has undefined effects, because you are converting a
> negative number ((signed long) (y - z) * 10) into an unsigned number in
> order to add to "x".
Not quite. The conversion from unsigned to signed int *is* well-defined.
Chapter and verse: ANSI, 3.2.1.2, lines 23-27:
"When a signed integer is converted to an unsigned integer with
equal or greater size, if the value of the signed integer is
nonnegative, its value is unchanged. Otherwise: if the unsigned
integer has greater size, the signed integer is first promoted to
the signed integer corresponding to the unsigned integer; the
value is converted to unsigned by adding to it one greater than
the largest number that can be represented in the unsigned integer
type. [footnote 28]"
[footnote 28] In a two's-complement representation, there is no
actual change in the bit pattern except filling the high-order
bits with copies of the sign bit if the unsigned integer has
greater size."
So an expression like "(unsigned int) 1 + -10" is officially
well-defined and has the value UINT_MAX - 8.
The irony is that an expression such as "(unsigned int) 10 + -10"
trips over integer overflow and is hence officially undefined, though
most implementations don't notice integer overflow and will "do what
you expect" by producing a result of 0.
> As the feeble joke in the compiler community goes, the compiler is free at
> this point to sing the star-spangled banner.. (or abort with a sign overflow
> error, or compute a bogus result, or ...)
I always write my compiler to assume that the user meant to write
"Hello world".
- Nathan
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palm.com/devzone/mailinglists.html