I create an unsigned int and unsigned char. Then I assign the -10 value, and the char remains unsigned and gives me a value of 246, but the unsigned int takes the -10 value.
int main ()
unsigned char a;
unsigned int b;
This is undefined behavior when you pass unsigned integers to
%d. Wrong format specifier is UB.
If you assign a negative value to an unsigned variable, it's fine and the value will be taken modulo
UINT_MAX + 1 (or
UCHAR_MAX + 1), so
(-10) % (UCHAR_MAX + 1) = 256 - 10 = 246, and
b is 4294967296 - 10 = 4294967286. Unsigned integral overflow is required to wrap-around.
printf is interpreting these numbers, it finds 246 is suitable for
%d, the format specifier for
signed int, and 4294967286 is reinterpreted as -10. That's all.