-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-print_all.c
98 lines (80 loc) · 1.61 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
#include "variadic_functions.h"
#include <stdio.h>
#include <stdarg.h>
void print_char(va_list arg);
void print_int(va_list arg);
void print_float(va_list arg);
void print_string(va_list arg);
void print_all(const char * const format, ...);
/**
* print_char - this function that Prints a char.
* @arg: A list of arguments.
*/
void print_char(va_list arg)
{
printf("%c", va_arg(arg, int));
}
/**
* print_int - this function that Prints an intger.
* @arg: A list of arguments.
*/
void print_int(va_list arg)
{
printf("%d", va_arg(arg, int));
}
/**
* print_float - this function that Prints a float.
* @arg: A list of arguments.
*/
void print_float(va_list arg)
{
printf("%f", va_arg(arg, double));
}
/**
* print_string - this function that Prints a string.
* @arg: A list of arguments.
*/
void print_string(va_list arg)
{
char *string;
string = va_arg(arg, char *);
if (string == NULL)
{
printf("(nil)");
return;
}
printf("%s", string);
}
/**
* print_all - this function that Prints anything, followed by a new line.
* @format: A string of characters.
* @...: A variable number of arguments to be printed.
*/
void print_all(const char * const format, ...)
{
va_list args;
int i = 0, j = 0;
char *separator = "";
printer_t funcs[] = {
{"c", print_char},
{"i", print_int},
{"f", print_float},
{"s", print_string}
};
va_start(args, format);
while (format && format[i] != '\0')
{
j = 0;
while (j < 4 && (format[i] != *(funcs[j].symbol)))
j++;
if (j < 4)
{
printf("%s", separator);
funcs[j].print(args);
separator = ", ";
}
i++;
}
printf("\n");
va_end(args);
}