-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
53 lines (49 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmaryjo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/05 18:41:48 by pmaryjo #+# #+# */
/* Updated: 2021/09/05 18:41:49 by pmaryjo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void print(char *digits, int size, int fd, int is_minus)
{
if (is_minus)
{
ft_putchar_fd('-', fd);
}
while (size >= 0)
{
ft_putchar_fd(digits[size--], fd);
}
}
void ft_putnbr_fd(int n, int fd)
{
int counter;
char digits[11];
long int num;
int is_minus;
counter = 0;
num = n;
is_minus = 0;
if (num == 0)
{
ft_putchar_fd('0', fd);
return ;
}
if (num < 0)
{
num *= -1;
is_minus = 1;
}
while (num)
{
digits[counter++] = num % 10 + '0';
num /= 10;
}
print(digits, counter - 1, fd, is_minus);
}