-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_putnbr_fd.c
More file actions
52 lines (47 loc) · 1.35 KB
/
ft_putnbr_fd.c
File metadata and controls
52 lines (47 loc) · 1.35 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbiasuz <lbiasuz@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/23 15:08:35 by lbiasuz #+# #+# */
/* Updated: 2022/04/26 13:05:42 by lbiasuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int int_size(int n)
{
unsigned int i;
i = 0;
if (n == 0)
return (2);
else if (n < 0)
i++;
while (n)
{
i++;
n = n / 10;
}
return (i);
}
void ft_putnbr_fd(int n, int fd)
{
char str[12];
int neg;
unsigned int i;
neg = n < 0;
ft_bzero(str, 12);
i = int_size(n) - 1;
if (n == 0)
str[0] = '0';
while (n)
{
str[i] = ((-(neg) + !neg) * (n % 10)) + 48;
n = n / 10;
i--;
}
if (neg)
str[i] = '-';
write(fd, str, ft_strlen(str));
}