The strtoul()
function will convert to an unsigned long
(which must have at least as much range as unsigned int
), and allows you to detect out-of-range values. You can further compare the result against UINT_MAX
(from <limits.h>
) to determine if it is within range of unsigned int
:
unsigned long result;
errno = 0;
result = strtoul(my_array, NULL, 10);
if ((result == ULONG_MAX && errno == ERANGE) || result > UINT_MAX)
{
/* Out of range of unsigned int */
}
else
{
/* Within range of unsigned int */
}