There is no undefined behavior here -- assuming that int
is wider than 16 bits.
This:
ss *= (short) -1;
is equivalent to:
ss = ss * (short)-1;
Both operands of *
are promoted from short
to int
(by the integer promotions), and the multiplication is done in type int
. It yields the int
value 32768
(again, unless INT_MAX == 32767
, which is legal but rare in modern non-embedded systems). (C has no arithmetic operations on integer types narrower than int
and unsigned int
.)
That int
value is converted back to short
. Unlike arithmetic operations, a conversion of an integer value to a signed integer result, when the value does not fit in the target type, yields an implementation-defined result (or raises an implementation-defined signal, but I don't think any implementations do that).
Converting 32768
to type short
will probably yield -32768
.
The behavior of signed conversion is specified in N1570 6.3.1.3p3:
Otherwise, the new type is signed and the value cannot be represented
in it; either the result is implementation-defined or an
implementation-defined signal is raised.
The integer promotions are described in 6.3.1.1p2:
If an int
can represent all values of the original type (as restricted
by the width, for a bit-field), the value is converted to an int
;
otherwise, it is converted to an unsigned int
. These are called the
integer promotions. All other types are unchanged by the integer promotions.