-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-print_all.c
113 lines (94 loc) · 1.96 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
#include <stdarg.h>
#include <stdio.h>
#include "variadic_functions.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 - Prints a char.
* @arg: A list of arguments pointing to
* the character to be printed.
*/
void print_char(va_list arg)
{
char letter;
letter = va_arg(arg, int);
printf("%c", letter);
}
/**
* print_int - Prints an int.
* @arg: A list of arguments pointing to
* the integer to be printed.
*/
void print_int(va_list arg)
{
int num;
num = va_arg(arg, int);
printf("%d", num);
}
/**
* print_float - Prints a float.
* @arg: A list of arguments pointing to
* the float to be printed.
*/
void print_float(va_list arg)
{
float num;
num = va_arg(arg, double);
printf("%f", num);
}
/**
* print_string - Prints a string.
* @arg: A list of arguments pointing to
* the string to be printed.
*/
void print_string(va_list arg)
{
char *str;
str = va_arg(arg, char *);
if (str == NULL)
{
printf("(nil)");
return;
}
printf("%s", str);
}
/**
* print_all - Prints anything, followed by a new line.
* @format: A string of characters representing the argument types.
* @...: A variable number of arguments to be printed.
*
* Description: Any argument not of type char, int, float,
* or char * is ignored.
* If a string argument is NULL, (nil) is printed instead.
*/
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)))
{
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);
}