libft
Loading...
Searching...
No Matches
ft_atoi.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_atoi.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tspoof <tspoof@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2022/11/02 14:03:13 by tspoof #+# #+# */
9/* Updated: 2022/11/30 16:45:21 by tspoof ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "libft.h"
14
15int ft_atoi(const char *str)
16{
17 int i;
18 int sign;
19 unsigned long val;
20
21 sign = 1;
22 val = 0;
23 i = 0;
24 while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32)
25 i++;
26 if (str[i] == '-')
27 sign = -1;
28 if (str[i] == '-' || str[i] == '+')
29 i++;
30 while (str[i] >= '0' && str[i] <= '9' && str[i] != '\n')
31 {
32 val = val * 10 + (str[i] - '0');
33 if (sign == 1 && val > 9223372036854775807)
34 return (-1);
35 if (sign == -1 && val > 9223372036854775807)
36 return (0);
37 i++;
38 }
39 return ((int)(sign * val));
40}
int ft_atoi(const char *str)
ASCII to integer.
Definition ft_atoi.c:15