Testing for a maximum unsigned value

后端 未结 6 1168
死守一世寂寞
死守一世寂寞 2020-12-19 17:02

Is this the correct way to test for a maximum unsigned value in C and C++ code:

if(foo == -1)
{
    // at max possible value
}

where foo is

6条回答
  •  不知归路
    2020-12-19 17:16

    I would define a constant that would hold the maximum value as needed by the design of your code. Using "-1" is confusing. Imagine that someone in the future will change the type from unsigned int to int, it will mess your code.

提交回复
热议问题