-
Notifications
You must be signed in to change notification settings - Fork 0
/
_print_unsigned.c
65 lines (53 loc) · 1.12 KB
/
_print_unsigned.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
#include "main.h"
/**
* printU - prints and unsigned integer
* @num: number
* @f: flag pointer
* Return: digits count
*/
int printU(unsigned long int num, flag *f)
{
int len = 0, i, padding, count = 0;
char buffer[20];
if (num == 0)
{
len = 1;
padding = f->width - len;
if (!f->isLeft)
count += _print_padding(padding);
count += _putchar('0');
if (f->isLeft)
count += _print_padding(padding);
return (count);
}
while (num > 0)
{
buffer[len++] = (num % 10) + '0';
num /= 10;
}
padding = f->width - len;
if (!f->isLeft)
count += _print_padding(padding);
for (i = len - 1; i >= 0; i--)
count += _putchar(buffer[i]);
if (f->isLeft)
count += _print_padding(padding);
return (count);
}
/**
* _print_unsigned - prints an integer
* @argPtr: argument pointer
* @f: flag ptr
* Return: digits count
*/
int _print_unsigned(va_list argPtr, flag *f)
{
unsigned long int num;
if (f->length == 'l')
num = va_arg(argPtr, unsigned long int);
else if (f->length == 'h')
num = (unsigned short int)va_arg(argPtr, unsigned int);
else
num = va_arg(argPtr, unsigned int);
return (printU(num, f));
}