-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_functions.c
99 lines (78 loc) · 1.55 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
#include "main.h"
/**
* print_char - cfr description
* @ap: input list of args
* Description: print a character
* Return: a number of a character printed
*/
int print_char(va_list ap)
{
char c = (char) va_arg(ap, int);
return (_putchar(c));
}
/**
* print_string - cfr description
* @ap: input list of args
* Description: print a string
* Return: a number of a character printed
*/
int print_string(va_list ap)
{
int j, nb_c = 0;
char *s = va_arg(ap, char *);
char buffer[1024];
if (!(s))
return (write(1, "(null)", 6));
for (j = 0; s[j] != '\0' && j < 1024; j++)
buffer[j] = s[j];
nb_c += write(1, &buffer, j);
for (; s[j] != '\0'; j++)
nb_c += _putchar(s[j]);
return (nb_c);
}
/**
* print_percent - cfr description
* @ap: input list of args
* Description: print the percent character
* Return: a number of a character printed
*/
int print_percent(va_list ap)
{
(void) ap;
return (_putchar('%'));
}
/**
* print_last_digit - cfr description
* @n: input integer
* Description: print the last digit of a number recursively
* Return: it's a void function
*/
void print_last_digit(unsigned int n)
{
if (n / 10)
print_last_digit(n / 10);
_putchar(n % 10 + '0');
}
/**
* print_int - cfr description
* @ap: input list of args
* Description: print an integer
* Return: a number of a integer printed
*/
int print_int(va_list ap)
{
int j = va_arg(ap, int), nb_c = 1;
unsigned int n = j;
if (j < 0)
{
nb_c += _putchar('-');
n = -n;
}
print_last_digit(n);
while (n / 10)
{
nb_c++;
n = n / 10;
}
return (nb_c);
}