Update
Can anyone explain the reason for promoting uint8_t to int? What are the use cases for this behavior? And can I prevent the promoting?
I have these very simple code snippets which add two integer types together and print the result. I have compiled those without any optimization with g++ --std=c++11 main.cpp -o test
int main()
{
    uint8_t a = 200, b = 100;
    uint16_t result = (a + b);
    printf("result is %d\n", result);
}
Outputs:
result is 300
I would have expected (a + b) to evaluate to 44 because they are both of an 8-bit type and should overflow. But instead I get the unexpected result of 300.
If I re-run the same test with uint32_t and uint64_t, it is overflowing as expected:
int main()
{
    uint32_t a = UINT_MAX, b = UINT_MAX;
    uint64_t result = (a + b);
    printf("result is %ld\n", result);
}
Outputs:
result is 4294967294
Now I can't tell why uint8_t is treated differently to uint32_t.
 
     
    