Search code examples
cinteger-promotioncompound-assignment

Why implicit conversion occurs for addition between unsigned char?


GCC warns this code:

unsigned char i = 1;
unsigned char j = 2;
i += j;

says:

warning: conversion to 'unsigned char' from 'int' may alter its value [-Wconversion]
   i += j;
        ^

It seems that j is implicitly converted to int.
Why the implicit conversion occurs for the addition of same-type variables?


Solution

  • 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)