-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_putnbr_fd.c
47 lines (42 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lsilva-q <lsilva-q@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/19 11:13:59 by lsilva-q #+# #+# */
/* Updated: 2021/09/22 15:15:29 by lsilva-q ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_putnbr_rec(unsigned int n, int *fd, int mult)
{
char c;
unsigned int tmp;
if (mult > 0)
{
c = '0' + (n / mult);
write(*fd, &c, 1);
tmp = n / mult;
tmp *= mult;
ft_putnbr_rec((n - tmp), fd, mult / 10);
}
return ;
}
void ft_putnbr_fd(int n, int fd)
{
long mult;
unsigned int nbr;
mult = 1;
while (n / (mult * 10) != 0)
mult *= 10;
if (n < 0)
{
write(fd, "-", 1);
nbr = n * -1;
}
else
nbr = n;
ft_putnbr_rec(nbr, &fd, mult);
}