-
Notifications
You must be signed in to change notification settings - Fork 1
/
print_fcns.c
135 lines (122 loc) · 1.85 KB
/
print_fcns.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "holberton.h"
/**
* pc - print char
* @valist: argument list
* Return: chars printed
*/
int pc(va_list valist)
{
char c;
c = va_arg(valist, int);
return (_putchar(c));
}
/**
* ps - print string
* @valist: argument list
* Return: chars printed
*/
int ps(va_list valist)
{
char *str;
str = va_arg(valist, char *);
if (str == NULL)
str = "(null)";
if (str[0] == '\0')
return (-2);
return (_putstring(str));
}
/**
* pd - print decimal
* @valist: argument list
* Return: chars printed
*/
int pd(va_list valist)
{
int n, k = 0, i, a = 1000000000, nmin;
n = va_arg(valist, int);
if (n < 0 && n > INT_MIN)
{
k += _putchar('-');
n *= -1;
}
if (n == INT_MIN)
{
k += _putchar('-');
nmin = 214748364;
for (i = 0; i < 10; i++)
{
if (nmin / a != 0)
k += _putchar(((nmin / a) % 10) + '0');
a /= 10;
}
k += _putchar('8');
}
if (n == 0)
k += _putchar('0');
if (n != INT_MIN)
{
for (i = 0; i < 10; i++)
{
if (n / a)
{
k += _putchar(((n / a) % 10) + '0');
}
a /= 10;
}
}
return (k);
}
/**
* pu - print unsigned integer
* @valist: argument list
* Return: chars printed
*/
int pu(va_list valist)
{
unsigned int n, i, a = 1000000000;
int k = 0;
n = va_arg(valist, unsigned int);
if (n == 0)
k += _putchar('0');
for (i = 0; i < 10; i++)
{
if (n / a)
{
k += _putchar(((n / a) % 10) + '0');
}
a /= 10;
}
return (k);
}
/**
* px - print lower case hexidecimal
* @valist: argument list
* Return: chars printed
*/
int px(va_list valist)
{
unsigned int n;
int k = 0, c = 0;
char hex[10];
n = va_arg(valist, unsigned int);
if (n == 0)
k += _putchar('0');
while (n != 0)
{
hex[c] = n % 16;
n /= 16;
c++;
}
c--;
while (c >= 0)
{
if (hex[c] >= 0 && hex[c] <= 9)
k += _putchar(hex[c] + '0');
else
{
k += _putchar((hex[c] % 10) + 'a');
}
c--;
}
return (k);
}