-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_int.c
More file actions
58 lines (52 loc) · 1.48 KB
/
print_int.c
File metadata and controls
58 lines (52 loc) · 1.48 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
53
54
55
56
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sudaniel <sudaniel@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 12:18:22 by sudaniel #+# #+# */
/* Updated: 2024/10/29 07:13:42 by sudaniel ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int is_signed(int d);
int print_int(int d)
{
int count;
unsigned int temp;
unsigned int power;
count = is_signed(d);
if (count == -1)
return (count);
else
temp = count;
count = 0;
if (d < 0)
count++;
power = 1;
while (temp / power > 9)
power *= 10;
while (power > 0)
{
if (write(1, &"0123456789"[temp / power], 1) == -1)
return (-1);
count++;
temp %= power;
power /= 10;
}
return (count);
}
static int is_signed(int d)
{
unsigned int temp;
if (d < 0)
{
if (write(1, "-", 1) == -1)
return (-1);
temp = -d;
}
else
temp = d;
return (temp);
}