B
bartc
Ben Pfaff said:happy said:int atoi(char str[])
{
int i, num;
for( num = 0, i = 0 ; isdigit(str) ; i++)
num = num * 10 + str - '0' ;
return num;
}
Here I wnat to know is there any way to tell user if num goes out of
range of int ?
if (num > INT_MAX / 10) {
...handle overflow...
Let's say INT_MAX is 2147483647 for example. So you're doing:
if (num>214748364) ... overflow.
However this might not detect overflows for inputs of "2147483648" and
"2147483649" (assuming you're doing the test with at least one more digit in
hand).