Testing for a maximum unsigned value

c, c++, unsigned

Solution

For C++, I believe you should preferably use the `numeric_limits` template from the `<limits>` header :

if (foo == std::numeric_limits<unsigned int>::max())
    /* ... */

For C, others have already pointed out the `<limits.h>` header and `UINT_MAX`.

Apparently, "solutions which are allowed to name the type are easy", so you can have :

template<class T>
inline bool is_max_value(const T t)
{
    return t == std::numeric_limits<T>::max();
}

[...]

if (is_max_value(foo))
    /* ... */

Problem

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 an `unsigned int`, an `unsigned short`, and so on.

Original source