-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuint_print.c
49 lines (46 loc) · 1.15 KB
/
uint_print.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
#include "main.h"
/**
* uint_print - unsigned integer printing function
* @flag: flag characters for non-custom conversion specifiers
* @args: variable argument list
* @count: pointer to integer to store the count of characters printed
* @space: space character for non-custom conversion specifiers
* u - unsigned integer (unsigned int) parameter to print
*/
void uint_print(char __attribute__ ((unused)) flag, va_list args,
int *count, char __attribute__ ((unused)) space)
{
unsigned int u, i, u_len, num;
int j;
char *digits;
u = va_arg(args, unsigned int);
num = u; /* hold the value of u before passing it to count logic */
/* count number of digits */
u_len = 0;
while (u / 10 != 0)
{
u_len++;
u = u / 10;
}
u_len++;
/* dynamically allocate memory for digits array */
digits = malloc(sizeof(char) * (u_len + 1));
if (digits == NULL)
return;
/* convert unsigned integer to string of digits */
i = 0;
while (num / 10 != 0)
{
digits[i] = (num % 10) + '0';
num = num / 10;
i++;
}
digits[i] = (num % 10) + '0';
/* print digits in reverse order */
for (j = i; j >= 0; j--)
{
_putchar(digits[j]);
(*count)++;
}
free(digits);
}