-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_putnbr_fd.c
59 lines (54 loc) · 1.47 KB
/
ft_putnbr_fd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mskeleto <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/11 21:39:08 by mskeleto #+# #+# */
/* Updated: 2020/11/11 21:39:31 by mskeleto ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void print_nbr(int n, int count, int fd)
{
int i;
int zero;
char d;
i = count;
zero = 1;
d = n % 10 + 48;
while (i-- > 1)
zero *= 10;
while (i++ <= count && (zero != 0))
{
d = n / zero + 48;
n %= zero;
zero /= 10;
write(fd, &d, 1);
}
}
void ft_putnbr_fd(int n, int fd)
{
int number;
int count;
number = n;
count = 0;
if (n == -2147483648)
write(fd, "-2147483648", 11);
else
{
if (number < 0)
{
write(fd, "-", 1);
number *= -1;
n *= -1;
}
while (number > 0)
{
number = number / 10;
count++;
}
print_nbr(n, count, fd);
}
}