-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_functions.c
131 lines (122 loc) · 1.95 KB
/
print_functions.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
#include "holberton.h"
/**
* print_char - writes the character c to stdout
* @c: The character to print
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
**/
int print_char(va_list c)
{
char x = va_arg(c, int);
write(1, &x, 1);
return (1);
}
/**
* print_string - write the string s to stdout
* @s: the string to print
* Return: on succes number of bytes printed
**/
int print_string(va_list s)
{
int count;
char *str;
str = va_arg(s, char*);
if (str == NULL)
str = "(null)";
for (count = 0; str[count]; count++)
{
write(1, &str[count], 1);
}
return (count);
}
/**
* print_perc - print percentage
* Return: 1
**/
int print_perc(void)
{
write(1, "%", 1);
return (1);
}
/**
* print_int - print integers
* @x: integer to print
* Return: number of byte printed
**/
int print_int(va_list x)
{
int i = 1, j, divi = 1, n = va_arg(x, int), nn = 0;
unsigned int m = n;
unsigned int bck = n;
if (n >= 0 && n <= 9)
_putchar('0' + n);
else if (n < 0 && n >= -9)
{
n = -n;
i++;
_putchar('-');
_putchar('0' + n);
}
else
{
if (n <= -10)
{
n = -n, nn = 1;
_putchar('-');
bck = n;
m = n;
}
while (m >= 10)
{
m = m / 10;
i++;
}
for (j = i; j > 1; j--)
divi = divi * 10;
for (j = 1; j <= i; j++)
{
m = bck / divi;
bck = bck - (m * divi);
divi = divi / 10;
_putchar('0' + m);
}
}
if (nn == 1)
return (i + 1);
return (i);
}
/**
* print_bin - print number to binary
* @n: number to print
* Return: number of bytes printed
**/
int print_bin(va_list n)
{
unsigned int x = va_arg(n, int);
int res, i, j, count = 0;
char *ptr;
unsigned int y = x;
if (x < 1)
{
_putchar('0' + 0);
return (1);
}
for (j = 0; y > 0; j++)
y = y / 2;
ptr = malloc(sizeof(char) * j);
if (ptr == NULL)
return (-1);
for (i = 0; x > 0; i++)
{
res = x % 2;
x = x / 2;
ptr[i] = res;
count++;
}
for (; i > 0; i--)
{
_putchar('0' + ptr[i - 1]);
}
free(ptr);
return (count);
}