-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf_x.c
More file actions
52 lines (41 loc) · 1016 Bytes
/
printf_x.c
File metadata and controls
52 lines (41 loc) · 1016 Bytes
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
#include "main.h"
/**
* printf_lowerHex - simultaneous convert and print integer in hexadecimal
* @num: the number to print in hexadecimal
* @count: pointer to counter of the number of bytes printed
*
* Return: void
*/
void printf_lowerHex(unsigned int num, int *count)
{
int retVal;
/* if num > 0xf, recursion */
if (num > 15)
printf_lowerHex(num >> 4, count);
/* break out of recursion if _putchar fails */
if (*count == -1)
return;
/* if num vs 0xf < 0xa, print 0-9 */
if ((num & 15) < 10)
retVal = _putchar('0' + (num & 15));
/* if num vs 0xf > 0xa, print a-f */
else
retVal = _putchar('a' + (num & 15) % 10);
/* if putchar fails, returns -1 */
if (retVal == -1)
*count = -1;
else
*count += retVal;
}
/**
* printf_x - print an integer in lowercase hexadecimal
* @args: va_list with integer to print as current element
*
* Return: number of bytes printed
*/
int printf_x(va_list args)
{
int count = 0;
printf_lowerHex(va_arg(args, int), &count);
return (count);
}