-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-print_all.c
87 lines (84 loc) · 1.35 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
#include "variadic_functions.h"
#include <stdarg.h>
#include <stdio.h>
/**
* tchar - prints variadic argument char
* @list: variadic list
*
* Return: No return
*/
void tchar(va_list list)
{
printf("%c", va_arg(list, int));
}
/**
* tint - prints variadic argument int
* @list: variadic list
*
* Return: No return
*/
void tint(va_list list)
{
printf("%i", va_arg(list, int));
}
/**
* tfloat - prints variadic argument float
* @list: variadic list
*
* Return: No return
*/
void tfloat(va_list list)
{
printf("%f", va_arg(list, double));
}
/**
* tstring - prints variadic argument string
* @list: variadic list
*
* Return: No return
*/
void tstring(va_list list)
{
char *tmp;
tmp = va_arg(list, char *);
if (tmp == 0)
tmp = "(nil)";
printf("%s", tmp);
}
/**
* print_all - prints anything
* @format: list of types of arguments passed to the function
* @...: Arguments Variadic
*
* Return: No return
*/
void print_all(const char * const format, ...)
{
ftype fa[] = {
{"c", tchar},
{"i", tint},
{"f", tfloat},
{"s", tstring}
};
int l1 = 0, l2 = 0;
va_list list;
char *comma = "";
va_start(list, format);
while (format && format[l1])
{
l2 = 0;
while (l2 < 4)
{
if (format[l1] == fa[l2].tc[0])
{
printf("%s", comma);
fa[l2].tf(list);
comma = ", ";
}
l2++;
}
l1++;
}
printf("\n");
va_end(list);
}