for every int x: x+1 > x … is this always true?

后端 未结 4 1898
鱼传尺愫
鱼传尺愫 2021-01-18 00:27

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

4条回答
  •  鱼传尺愫
    2021-01-18 00:47

    Note for 32-bit number range is [-2147483648, 2147483647] that is equals to [-231, 231 -1 ].

    So for expression x+1 > x is true for [-2147483648, 2147483646]

    But not for 2147483647 because adding to 2147483647 in 32-bit size number causes bit overflow many implementations it makes x + 1 to -2147483648 But really behavior is Undefined in C standard.

    So,

    • x + 1 > x True for x in [-2147483648, 2147483646] only
    • x + 1 > x , for x = 2147483647 is Undefined value may be True or False depends on compiler. If a compiler calculates = -2147483648 value will be False.

提交回复
热议问题