There is code:
#include <iostream>
int main(){
unsigned char a = 4, b = 255;
int g = (unsigned char)a + (unsigned char)b;
std::cout << g << std::endl;
return 0;
}
Result:
259
Why the result is 259, not 3? If there are added two unsigned char variables, there should be overflow, result should be 3 and then it should convert from unsigned char 3 to int 3.
The addition operation will first promote its operands to
int, before doing the addition. This is how C works. If you want to truncate, you need to assign it back into a narrower type, such asunsigned char.