#Integer promotion in C

13 messages · Page 1 of 1 (latest)

vale veldt
#

Say i have:

uint16_t u16d = 30000;
uint32_t u32e = u16c + u16d;

On a 32-bit machine, would u16c and u16d be automatically promoted to an integer type?
Also side question, which types does integer promotion apply to? is it only char, short, int, and long?

dreamy vigilBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

wooden zenith
#

so if int is more than 16 bits, u16c + u16d will do the addition in int; otherwise int is 16 and it will do it in unsigned

#

so is int is 17 bits the addition will overflow

#

to get the overflow to not happen here you should cast to uint32_t on at least one of the operands

#

for a 32 bit machine which you're asking for it will promote to int (and successfully obtain the value 70000)

wooden zenith
vale veldt
#

thank you that makes perfect sense! i just had one final question in terms of registers/cpu arithmetic - and how the rhs of the operation would be stored. but let me just quickly see if i can figure it out from the disassembly code.

#

okay am i correct in saying that in a 32-bit cpu (or 32-bit register, not sure on the correct term). the cpu would take Register1 (where u16c lives), add the value of Register3 (where u16d lives) and then stores that sum in Register1 temporarily?

(register 1 and 3 are arbitrary here i guess)

dreamy vigilBOT
#

@vale veldt Has your question been resolved? If so, type !solved :)

#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

tulip cedar