#Integer promotion in C
13 messages · Page 1 of 1 (latest)
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.
for integer types (other than C2X bit-precise types), if their conversion rank is less than that of int then they are implicitly converted in arithmetic expressions like so: if the type int is capable of representing all of the values of the type then it is converted to int, otherwise unsigned
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)
if int was 16 bits it would result in unexpected value as well (4464)
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)
@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
In a 32 bit CPU, the registers are typically 32 bits wide, meaning they can old 32 bits of data
when you performing arithmetic operations, like addition, the CPU uses these registers to store the operands and the result
in your example above, the actual register allocation and usage depending on the specific CPU architecture and the compilers optimization decisions