-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/11 20:08:00 by gmachado #+# #+# */
/* Updated: 2022/04/18 20:10:46 by gmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <unistd.h>
#define MAX_CHARS 11
#define MAX_DIVIDER 1000000000
void ft_putnbr_fd(int n, int fd)
{
int divider;
char digit;
if (fd < 0)
return ;
divider = MAX_DIVIDER;
if (n == 0)
{
write(fd, "0", 1);
return ;
}
if (n < 0)
{
write(fd, "-", 1);
divider = -divider;
}
while (divider != 0 && n / divider == 0)
divider /= 10;
while (divider != 0)
{
digit = '0' + (char)(n / divider);
write(fd, &digit, 1);
n %= divider;
divider /= 10;
}
}