If current_time
is greater than or equal to last_time
, there won't be potential for a negative value.
To quote section 6.2.1.2 "Signed and unsigned integers" of the C90 spec:
When a value with integral type is converted to another integral type, if the value can be represented by the new type, its value is unchanged.
With unsigned short
being shorter than int
, all values of type unsigned short
can be represented by int
, so the converted-to-int
values of current_time
and last_time
will be the same as their un-converted unsigned short
value, and the result of the subtraction will be what you expect it to be.
If, however, current_time
is less than last_time
, there is potential for a negative value; that, however, is not a bug, it's a feature, because, in that case, time really did go backwards.
If current_time
and last_time
were unsigned int
s, and current_time
were less than last_time
, the result of the subtraction would be an unsigned int
, and thus not negative; it would be the difference between the times, modulo the maximum value of an unsigned int
.
You probably want to handle time going backwards specially, anyway, if it can happen.