-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_putint.c
59 lines (52 loc) · 1.41 KB
/
ft_putint.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cabo-ram <cabo-ram@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/28 11:47:27 by cabo-ram #+# #+# */
/* Updated: 2024/11/07 17:30:31 by cabo-ram ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t int_digits(int n)
{
size_t i;
i = 0;
if (n == 0)
return (1);
if (n < 0)
n = -n;
while (n > 0)
{
n = n / 10;
i++;
}
return (i);
}
static void print_int(int n)
{
static char digits[] = "0123456789";
if (n >= 10)
print_int(n / 10);
ft_putchar(digits[n % 10]);
}
int ft_putint(int n)
{
size_t len;
if (n == -2147483648)
{
write (1, "-2147483648", 11);
return (11);
}
len = int_digits(n);
if (n < 0)
{
write (1, "-", 1);
n = -n;
len++;
}
print_int (n);
return (len);
}