-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable_args_list.c
42 lines (37 loc) · 936 Bytes
/
variable_args_list.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
#include <stdio.h>
#include <stdarg.h>
void foo(char *fmt, ...)
{
va_list ap, ap2;
int d;
char c, *s;
va_start(ap, fmt);
va_copy(ap2, ap);
while (*fmt)
switch (*fmt++)
{
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c': /* char */
/* Note: char is promoted to int. */
c = va_arg(ap, int);
printf("char %c\n", c);
break;
}
va_end(ap);
// ...
// use ap2 to iterate over the arguments again
// ...
va_end(ap2);
}
int main(int argc, char const *argv[])
{
/* code */
return 0;
}