r/C_Programming • u/Low_Minimum9920 • Jun 15 '26
Question Question regarding unsigned integers
What's the difference between an unsigned int and a normal integer?
11
Upvotes
r/C_Programming • u/Low_Minimum9920 • Jun 15 '26
What's the difference between an unsigned int and a normal integer?
View all comments
Show parent comment
0
u/flatfinger Jun 16 '26
The vast majority of optimizing transforms that would be impeded by using precise two's-complement wrapping semantics would be allowed under rules that allowed compilers to use larger than specified types to hold temporary results, but treated overflow as being defined except for that provision. A few more could be allowed if one extended this allowance to situations where division using an oversized value could trap (e.g. computing int1*int2/int3 without truncating the product, in cases where the quotient wouldn't fit within the range of int).
A few more optimizations would be allowed by allowing an attempt to store an oversized value in an automatic-duration object whose address isn't taken to actually store such a value without truncation. Note that the value of types like
int_fast16_twould be enhanced if addressable values of such type could use two bytes while ADOWAIT of such type could use 32-bit registers without having to sign-extend 16-bit values stored to them.The authors of the Standard expected that except when targeting unusual execution environments, coercing the result of an addition, subtraction, multiplication, left-shift, or bitwise operator to
unsigned intwould yield the same behavior as coercing the operands likewise. The reason the Standard doesn't specify that is that the authors of the Standard never imagined that implementations for any common execution environments would have any reason to do anything else.