I understand typecasting…but only in retrospect. My process to figure out what requires typecasting in expressions is usually retroactive because I can’t predict when it will be required because I don’t know how the compiler steps through them. A somewhat trite example:
int8_t x = -50;
uint16_t y = 50;
int32_t z = x * y;
On my 8-bit processor (Freescale HCS08) sets z to 63036 (2^16 – 50^2). I can see how that would be one possible answer (out of maybe 4 others), but I would not have guessed it would be the one.
A better way to ask might be: when types interact with operators (+-*/), what happens?
The folks here that say that values are always converted to the larger type are wrong. We cannot talk about anything if we don’t know your platform (I see you have provided some information now). Some examples
This results in value
-2500because both operands are converted toint, and the operation is carried out signed and the signed result is written to anint32_t.This results in value 63036 because the
int8_toperand is first converted tounsinged int, resulting in65536-50. It is then multiplied with it, resulting in3 274 300 % 65536(unsigned is modulo arithmetic) which is 63036. That result is then written toint32_t.Notice that the minimum
intbit-size is16bits. So on your 8-bit platform, this second scenario is what likely happens.I’m not going to try and explain the rules here because it doesn’t make sense to me to repeat what is written in the Standard / Draft (which is freely available) in great detail and which is usually easily understandable.