-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_num.c
72 lines (65 loc) · 1.66 KB
/
write_num.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* write_num.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: daniema3 <daniema3@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:25:32 by daniema3 #+# #+# */
/* Updated: 2024/10/01 16:25:33 by daniema3 ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_ultoa(unsigned long nb)
{
int digits;
unsigned long n;
char *res;
digits = 1;
n = nb;
while (n > 9)
{
n /= 10;
digits++;
}
res = malloc((digits + 1) * sizeof(char));
if (!res)
return (NULL);
res[digits] = '\0';
digits--;
if (nb == 0)
res[0] = '0';
while (digits >= 0)
{
res[digits] = ('0' + (nb % 10));
nb /= 10;
digits--;
}
return (res);
}
int write_num(int nb)
{
int res;
char *num;
res = 0;
if (nb == INT_MIN)
return (write(1, "-2147483648", 11));
if (nb < 0)
{
res += write(1, "-", 1);
nb = nb * -1;
}
num = ft_ultoa(nb);
res += write_str(num);
free(num);
return (res);
}
int write_unum(unsigned int nb)
{
int res;
char *num;
num = ft_ultoa(nb);
res = write_str(num);
free(num);
return (res);
}