forked from Monemax94/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_nums.c
85 lines (75 loc) · 1.45 KB
/
print_nums.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "main.h"
/**
* print_int - Prints an integer to the standard output
* @l: va_list of arguments from _printf
* @f: pointer to the struct flags determining
* if a flag is passed to _printf
* Return: number of char printed
*/
int print_int(va_list l, flags_t *f)
{
int n = va_arg(l, int);
int res = count_digit(n);
if (f->space == 1 && f->plus == 0 && n >= 0)
res += _putchar(' ');
if (f->plus == 1 && n >= 0)
res += _putchar('+');
if (n <= 0)
res++;
print_number(n);
return (res);
}
/**
* print_unsigned - prints an unsigned integer
* @l: va_list of arguments from _printf
* @f: pointer to the struct flags determining
* if a flag is passed to _printf
* Return: number of char printed
*/
int print_unsigned(va_list l, flags_t *f)
{
unsigned int u = va_arg(l, unsigned int);
char *str = convert(u, 10, 0);
(void)f;
return (_puts(str));
}
/**
* print_number - helper function that loops through
* an integer and prints all its digits
* @n: integer to be printed
*/
void print_number(int n)
{
unsigned int n1;
if (n < 0)
{
_putchar('-');
n1 = -n;
}
else
n1 = n;
if (n1 / 10)
print_number(n1 / 10);
_putchar((n1 % 10) + '0');
}
/**
* count_digit - returns the number of digits in an integer
* for _printf
* @i: integer to evaluate
* Return: number of digits
*/
int count_digit(int i)
{
unsigned int d = 0;
unsigned int u;
if (i < 0)
u = i * -1;
else
u = i;
while (u != 0)
{
u /= 10;
d++;
}
return (d);
}