I\'m trying to subtract two unsigned ints and compare the result to a signed int (or a literal). When using unsigned int
types the behavior is as expected. Wh
Before both the -
and <
operations are performed, a set of conversions called the usual arithmetic conversions are applied to convert the operands to a common type. As part of this process, the integer promotions are applied, which promote types narrower than int
or unsigned int
to one of those two types.
In the first case, the types of a
and b
are unsigned int
, so no change of types occurs due to the -
operator - the result is an unsigned int
with the large positive value UINT_MAX - 14
. Then, because int
and unsigned int
have the same rank, the value 10
with type int
is converted to unsigned int
, and the comparison is then performed resulting in the value 0
.
In the second case, it is apparent that on your implementation the type int
can hold all the values of the type uint16_t
. This means that when the integer promotions are applied, the values of a
and b
are promoted to type int
. The subtraction is performed, resulting in the value -15
with type int
. Both operands to the <
are already int
, so no conversions are performed; the result of the <
is 1
.
When you use a 10U
in the latter case, the result of a - b
is still -15
with type int
. Now, however, the usual arithmetic conversions cause this value to be converted to unsigned int
(just as the 10
was in the first example), which results in the value UINT_MAX - 14
; the result of the <
is 0
.