UINT_MAX + 1 equals what?

后端 未结 4 2002
执念已碎
执念已碎 2020-12-15 08:28

What is the defined behavior in C for UINT_MAX + 1u? How safe is to assume it is zero?

4条回答
  •  隐瞒了意图╮
    2020-12-15 09:01

    From the standard (C11, 6.2.5/9, emphasis mine):

    [...] A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

    If UINT_MAX is 10:

    (10 + 1) % (10 + 1) == 0
    

    So, yes, it's safe to assume it's zero.

提交回复
热议问题