-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.c
81 lines (70 loc) · 1.25 KB
/
int.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "main.h"
/**
* print_int - a function that print an int
*
* @args: arguments
*
* @buffer: buffer to store result before printing
*
* Return: int (number of digits printed)
*/
int print_int(va_list args, char *buffer)
{
int n_10 = 1; /*n_10 : number of iterations by dividing by 10 */
int i, digit;
unsigned int n;
int nop = va_arg(args, int);
i = 0;
if (nop < 0)
{
buffer[i++] = '-';
n = (-1) * nop;
}
else
n = nop;
while (n / n_10 > 9)
n_10 *= 10;
while (n_10 > 0)
{
digit = n / n_10;
buffer[i++] = digit + '0';
n = n - (n_10 * digit);
n_10 /= 10;
}
return (flush_buffer(buffer));
}
/**
* print_addr - prints a memory address
*
* @args: list of variable arguments
*
* @buffer: store the characters to print in reverse order
*
* Return: the number of characters written to stdout
*/
int print_addr(va_list args, char *buffer)
{
int i, r;
uintptr_t n;
void *ptr = va_arg(args, void *);
if (!ptr)
{
ptr = "(nil)";
return (write(1, ptr, _strlen(ptr)));
}
n = (uintptr_t)(ptr);
i = 0;
while (n > 0)
{
r = n % 16;
if (r < 10)
buffer[i++] = r + '0';
else
buffer[i++] = r + 87;
n /= 16;
}
i = _strlen(buffer);
buffer[i] = 'x';
buffer[i + 1] = '0';
return (flush_buffer(rev_str(buffer)));
}