-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableArguments.cpp
88 lines (76 loc) · 1.98 KB
/
VariableArguments.cpp
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
/*
* \file VariableArguments.cpp
* \brief
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
/**
* printVars
*
* takes a format string of the form "ifcs", where each character specifies the type of the
* argument in that position.
*
* i = int
* f = float
* c = char
* s = string (char *)
*
* Following the format specification is a variable list of arguments. Each argument corresponds to
* a format character in the format string to which the format parameter points
*/
void
printVars(const char *format, ...)
{
union Printer
{
int i;
float f;
char c;
char *s;
};
va_list vl;
// format is the last argument specified; you must access
// all others using the variable-argument macros.
va_start(vl, format);
// Step through the list.
for (std::size_t i = 0; format[i] != '\0'; ++ i) {
Printer printer;
switch (format[i]) {
case 'i':
printer.i = va_arg(vl, int);
printf("%i\n", printer.i);
break;
case 'f':
printer.f = va_arg(vl, double);
printf("%f\n", printer.f);
break;
case 'c':
// note automatic conversion to integral type
printer.c = va_arg(vl, int);
printf("%c\n", printer.c);
break;
case 's':
printer.s = va_arg(vl, char *);
printf("%s\n", printer.s);
break;
default:
break;
}
}
va_end(vl);
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
::printVars("fcsi", 32.4f, 'a', "Test string", 4);
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
32.400002
a
Test string
4
#endif