I\'m just starting to learn C at school, I\'m trying to get a hold of the basic concepts.
Our homework has a question,
for every int x: x+1 > x
Yes, x + 1 adds to the decimal value of 1.
This will be true almost all of the time. But if you add 1 to INT_MAX (which is 215 - 1 or greater), you might flip the sign. Think about the decimal representation of 0111111
versus 11111111
. (Obviously not 32 bits, but the ideas hold.)
Look up two's complement if you're confused about why it flips. It's a pretty clever implementation of integers that makes addition easy.
EDIT: INT_MAX + 1
is undefined behavior. Doesn't necessarily become INT_MIN
. But since x + 1
is not necessarily > x
when x == INT_MAX
, then the answer is clearly false!