hello I'm trying to recreate atoi in with some other added conditions, mainly computing how many signs and returning the number positive if number of minus even , otherwise negative, the script works fine. the problem it should be created with int type and it will be tested with some really big numbers, such as 19999999999 , I tried to cast it's return value but stupidly forget it will return always it's type. any idea ? thanks
#include <stdio.h>
int ft_checksign(char *str)
{
int i;
int sign;
sign = 0;
i = 0;
while (*(str + i))
{
if (*(str + i) == '-')
sign += 1;
i++;
}
return (sign);
}
int ft_atoi(char *str)
{
long long result;
int i;
i = 0;
result = 0;
while ((*(str + i) >= 9 && *(str + i) <= 32)
|| *(str + i) == '-' || *(str + i) == '+')
i++;
while (*(str + i) && (*(str + i) >= '0' && *(str + i) <= '9'))
{
result *= 10;
result += (long long)str[i] - '0';
i++;
}
if (ft_checksign(str) % 2 == 1)
return ((long long)-result);
else
return ((long long)result);
}
int main()
{
printf("%i", ft_atoi("19999999999"));
return 0;
}
OUTPUT
-1474836481
In the comments section above you're saying: "I was tasked making atoa big-numbers proof."
Luckily we have the
strtoxxfunctions but that is not the issue here.Lets take a look to the
atoidocumentation:Description
Return Value
Bugs
If you consider, that
inthas either 16 or 32 bits, you're limited to a certain range. Not to forget, thatintis a signed type, which means, that one bit is reserved for the sign.The problem you have to solve is simple: implement an
atoifunction, so that it is possible to distinguish between 0 or an error and implement checks for overflow and underflow. Sinceerrnois not set,errnois global, set it accordingly.From man
strtol:Return Value
Errors
And last but not least: strtol.c