-
Notifications
You must be signed in to change notification settings - Fork 3
/
print_add.c
52 lines (51 loc) · 1.22 KB
/
print_add.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
#include "main.h"
#include <stdio.h>
/**
* print_add - prints the address of an input variable
* @arguments: input address.
* @buf: buffer pointer.
* @ibuf: index for buffer pointer
*
* Return: number of chars printed.
*/
int print_add(va_list arguments, char *buf, unsigned int ibuf)
{
void *add;
long int int_input;
int i, count, first_digit, isnegative;
char *hexadecimal, *binary;
char nill[] = "(nil)";
add = (va_arg(arguments, void *));
if (add == NULL)
{
for (i = 0; nill[i]; i++)
ibuf = handl_buf(buf, nill[i], ibuf);
return (5);
}
int_input = (intptr_t)add;
isnegative = 0;
if (int_input < 0)
{
int_input = (int_input * -1) - 1;
isnegative = 1;
}
binary = malloc(sizeof(char) * (64 + 1));
binary = fill_binary_array(binary, int_input, isnegative, 64);
hexadecimal = malloc(sizeof(char) * (16 + 1));
hexadecimal = fill_hex_array(binary, hexadecimal, 0, 16);
ibuf = handl_buf(buf, '0', ibuf);
ibuf = handl_buf(buf, 'x', ibuf);
for (first_digit = i = count = 0; hexadecimal[i]; i++)
{
if (hexadecimal[i] != '0' && first_digit == 0)
first_digit = 1;
if (first_digit)
{
ibuf = handl_buf(buf, hexadecimal[i], ibuf);
count++;
}
}
free(binary);
free(hexadecimal);
return (count + 2);
}