-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprintf_pointer.c
More file actions
50 lines (43 loc) · 828 Bytes
/
printf_pointer.c
File metadata and controls
50 lines (43 loc) · 828 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
#include "main.h"
#define MAX_HEX_DIGITS 16
/**
* printf_pointer - prints a binary number
* @args: numberof arguements
* @printed: the printed characters
* Return: printed charcaters
*/
int printf_pointer(va_list args, int printed)
{
void *ptr = va_arg(args, void*);
unsigned long num = (unsigned long) ptr;
int digits = 0;
int i;
unsigned long temp = num;
char hex_digits[MAX_HEX_DIGITS] = "0123456789abcdef";
char hex[MAX_HEX_DIGITS];
while (temp != 0)
{
digits++;
temp /= 16;
}
printed += _putchar('0');
printed += _putchar('x');
if (num == 0)
{
printed += _putchar('0');
}
else
{
for (i = digits - 1; i >= 0; i--)
{
int digit = num % 16;
hex[i] = hex_digits[digit];
num /= 16;
}
for (i = 0; i < digits; i++)
{
printed += _putchar(hex[i]);
}
}
return (printed);
}