-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-print_all.c
120 lines (103 loc) · 1.86 KB
/
3-print_all.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
#include "variadic_functions.h"
/**
* print_char - print a char
*
* @arg: a list of argument pointing
* to the character to be printed
*
* Return: nothing
*/
void print_char(va_list arg)
{
char c = va_arg(arg, int);
printf("%c", c);
}
/**
* print_int - print an integer
*
* @arg: a list of argument pointing
* to the character to be printed
*
* Return: nothing
*/
void print_int(va_list arg)
{
int n = va_arg(arg, int);
printf("%d", n);
}
/**
* print_float - print a float
*
* @arg: a list of argument pointing
* to the character to be printed
*
* Return: nothing
*/
void print_float(va_list arg)
{
float n = va_arg(arg, double);
printf("%f", n);
}
/**
* print_string - print a string
*
* @arg: a list of argument pointing
* to the character to be printed
*
* Return: nothing
*/
void print_string(va_list arg)
{
char *str = va_arg(arg, char *);
if (str == NULL)
{
printf("(nil)");
return;
}
printf("%s", str);
}
/**
* print_all - a function that prints anything
*
* @format: A string of character representing
* the argument types
*
* Description: If any argument not of type char,
* int, float or char * is ignored
*
* Return: nothing
*/
void print_all(const char * const format, ...)
{
va_list ap;
int i = 0, j = 0;
char *separator = "";
func_printer funcs[] = {
{"c", print_char},
{"i", print_int},
{"f", print_float},
{"s", print_string}
};
va_start(ap, format);
while (format && format[i])
{
j = 0;
/**
* 4 equals to the number of funcs present
* so if j is less than four and our current
* format is not equal to format in funcs
* then j becomes j + 1
*/
while (j < 4 && (format[i] != *(funcs[j].symbol)))
j++;
if (j < 4)
{
printf("%s", separator);
funcs[j].print_func(ap);
separator = ", ";
}
i++;
}
printf("\n");
va_end(ap);
}