-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_int.c
More file actions
55 lines (49 loc) · 1.56 KB
/
print_int.c
File metadata and controls
55 lines (49 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhegedus <mhegedus@student.42vienna.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 22:00:21 by mhegedus #+# #+# */
/* Updated: 2024/11/21 15:10:21 by mhegedus ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdbool.h>
static int to_positive(int n)
{
if (n < 0)
return (-n);
else
return (n);
}
static void write_number_recursive(int n, int *char_count)
{
char c;
if (n / 10)
write_number_recursive(n / 10, char_count);
c = to_positive(n % 10) + '0';
*char_count += write(STDOUT_FILENO, &c, 1);
}
// prints integer n to stdout
// returns number of chars printed
int print_int(int n)
{
int char_count;
char_count = 0;
if (n < 0)
char_count += write(STDOUT_FILENO, "-", 1);
write_number_recursive(n, &char_count);
return (char_count);
}
/*
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc == 3)
{
ft_putnbr_fd(atoi(argv[1]), atoi(argv[2]));
}
}
*/