C mandates integer promotions, quoting C11 N1570/6.3.1.1p2
The following may be used in an expression wherever an int or unsigned int may be used:
- An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.
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
So your unsigned char
variable is used in such an expression and as such is converted to int
, since i += j
is functionally equivalent to i = i + j
.
If you know for certain the result is in the range of an unsigned char
, then just apply a cast to the result of the addition before assigning, like this:
i = (unsigned char)(i + j)