ft_itoa.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ft_itoa.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: ll-hotel <ll-hotel@student.42lyon.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/11/08 10:20:09 by ll-hotel #+# #+# */ 9 /* Updated: 2024/08/20 17:48:42 by ll-hotel ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "ft_basics.h" 14 #include "ft_math.h" 15 16 char *ft_itoa(int n) 17 { 18 const _Bool is_negative = (n < 0); 19 long tmp; 20 char *val; 21 short len; 22 short i; 23 24 len = ft_log_b(n, 10) + 1; 25 val = ft_calloc(len + 1 + is_negative, sizeof(char)); 26 if (!val) 27 return (0); 28 i = len + is_negative; 29 tmp = n * (!is_negative - is_negative); 30 while (i-- > is_negative) 31 { 32 val[i] = tmp % 10 + '0'; 33 tmp /= 10; 34 } 35 if (n < 0) 36 val[0] = '-'; 37 return (val); 38 }