r/C_Programming • • Jun 15 '26

Question Question regarding unsigned integers

What's the difference between an unsigned int and a normal integer?

11 Upvotes

93 comments sorted by

View all comments

Show parent comment

3

u/ffd9k Jun 15 '26

Try this with gcc or clang -O2 on your favorite modern architecture:

#include <limits.h>

int does_it_overflow_to_int_min(int x)
{
    return x + 1 == INT_MIN;
}

int main()
{
    return does_it_overflow_to_int_min(INT_MAX);
}

0

u/flatfinger Jun 16 '26

I don't view the computation of x+1 using a type larger than int as particularly astonishing (int1+1LL will always yield a value numerically higher than INT_MIN) . What's more astonishing, and goes directly contrary to the documented expectations of the Committee, are cases where gcc will process uint1=ushort1*ushort2; in ways that disrupt the behavior of surrounding code even if the only cases where uint1 is ever examined are those where the computation didn't overflow.