-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putunbr.c
62 lines (56 loc) · 1.77 KB
/
ft_putunbr.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
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putunbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tunsal <tunsal@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 16:44:02 by tunsal #+# #+# */
/* Updated: 2023/11/11 20:52:59 by tunsal ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
Longest integer we can receive is 4294967295 with length 10.
We need 11 characters including the null-terminator.
*/
#define U_BUFF_SIZE 11
static int count_digits_unsigned(unsigned int nb)
{
int digit_count;
if (nb == 0)
return (1);
digit_count = 0;
while (nb != 0)
{
nb /= 10;
++digit_count;
}
return (digit_count);
}
/*
Print an unsigned number to the standard out
and return number of characters printed.
Return -1 if write fails or upon any error.
*/
int ft_putunbr(unsigned int nb)
{
char buff[U_BUFF_SIZE];
int buff_idx;
size_t num_len;
ssize_t ret;
buff[0] = '0';
buff_idx = count_digits_unsigned(nb);
buff[buff_idx--] = '\0';
while (nb > 0)
{
buff[buff_idx] = '0' + (nb % 10);
nb /= 10;
--buff_idx;
}
num_len = ft_strlen(buff);
ret = write(STDOUT_FD, buff, num_len);
if (ret != (ssize_t) num_len)
return (-1);
return (ret);
}