-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-print_all.c
77 lines (70 loc) · 1.32 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
#include "variadic_functions.h"
/**
* _printchar - print char type element from va_list
* @list: va_list passed to function
*/
void _printchar(va_list list)
{
printf("%c", va_arg(list, int));
}
/**
* _printstr - print string element from va_list
* @list: va_list passed to function
*/
void _printstr(va_list list)
{
char *s;
s = va_arg(list, char *);
if (s == NULL)
s = "(nil)";
printf("%s", s);
}
/**
* _printfloat - print float type element from va_list
* @list: va_list passed to function
*/
void _printfloat(va_list list)
{
printf("%f", va_arg(list, double));
}
/**
* _printint - print int type element from va_list
* @list: va_list passed to function
*/
void _printint(va_list list)
{
printf("%d", va_arg(list, int));
}
/**
* print_all - prints anything.
* @format: list of types of arguments passed to the function
* Return: Nothing.
*/
void print_all(const char * const format, ...)
{
unsigned int i, j;
va_list args;
char *sep;
check_t storage[] = {
{ "c", _printchar },
{ "f", _printfloat },
{ "s", _printstr },
{ "i", _printint }
};
i = 0;
sep = "";
va_start(args, format);
while (format != NULL && format[i / 4] != '\0')
{
j = i % 4;
if (storage[j].type[0] == format[i / 4])
{
printf("%s", sep);
storage[j].f(args);
sep = ", ";
}
i++;
}
printf("\n");
va_end(args);
}