-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprint_unsigned.c
More file actions
106 lines (92 loc) · 2.24 KB
/
print_unsigned.c
File metadata and controls
106 lines (92 loc) · 2.24 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <limits.h>
#include "print_helpers.h"
/**
* print_unsigned_helper - Prints unsigned int
* @n: int number to print
* @len: length of bytes written
*
*/
static void print_unsigned_helper(unsigned int n, int *len)
{
int last_digit = 0;
char c = 0;
last_digit = n % 10;
if (n >= 10)
print_unsigned_helper(n / 10, len);
c = last_digit + '0';
*len += buffered_print(&c, 1);
}
/**
* print_short_unsigned_helper - Prints unsigned int
* @n: int number to print
* @len: length of bytes written
*
*/
static void print_short_unsigned_helper(unsigned short int n, int *len)
{
int last_digit = 0;
char c = 0;
last_digit = n % 10;
if (n >= 10)
print_short_unsigned_helper(n / 10, len);
c = last_digit + '0';
*len += buffered_print(&c, 1);
}
/**
* print_long_unsigned_helper - Prints unsigned int
* @n: int number to print
* @len: length of bytes written
*
*/
static void print_long_unsigned_helper(unsigned long int n, int *len)
{
int last_digit = 0;
char c = 0;
last_digit = n % 10;
if (n >= 10)
print_long_unsigned_helper(n / 10, len);
c = last_digit + '0';
*len += buffered_print(&c, 1);
}
/**
* updater - update number in num len function
* @num: number
*
* Return: updated number
*/
static unsigned long int updater(unsigned long int num)
{
return (num / 10);
}
/**
* print_unsigned - print unsigned int
* @spec: specifier object
* @arg: pointer to arguments to be printed
*
* Return: length of bytes written
*/
int print_unsigned(specifier_t *spec, va_list arg)
{
int len = 0;
unsigned long int num = va_arg(arg, unsigned long int);
unsigned int width = num_len(num, updater);
if (!(spec->flags & FLAG_LEFT))
{
if (width < spec->width)
len += print_nchar(spec->flags & FLAG_ZERO ? '0' : ' ',
spec->width - width);
if (width < spec->precision)
len += print_nchar('0', spec->precision - width);
if (spec->flags & FLAG_PRECISION && spec->precision == 0 && num == 0)
return (len);
}
if (spec->flags & FLAG_LONG)
print_long_unsigned_helper(num, &len);
else if (spec->flags & FLAG_SHORT)
print_short_unsigned_helper(num, &len);
else
print_unsigned_helper(num, &len);
if (width < spec->width && spec->flags & FLAG_LEFT)
len += print_space(spec->width - width);
return (len);
}