Possible Duplicate:
C integer overflow behaviour when assigning to larger-width integers
I haven't found a clear answer on this in my googling. Say you have two expressions:
int16_t a16 = 100;
int16_t b16 = 2000;
int16_t result16 = (a16 * b16) / a16;
int8_t a8 = 100;
int8_t b8 = 20;
int8_t result8 = (a8* b8) / a8;
When evaluation the expressions (a16 * b16) / a16
and (a8 * b8) / a8
, are they always promoted to int
during the evaluation and then the final result is converted back to the desired type (int16_t
or int8_t
) just before the assignment, or is this integer promotion entirely optional? Is integer promotion while evaluating an integer expression always done, or is it just simply allowed?
If it's always done, then I can expect the two operations to not overflow (assuming int
is 32-bits). If it's only allowed to be done (and not required), then the operations may overflow. I'd like to know the behavior better.