0
|
1 |
/*
|
|
2 |
* $Id: varargs.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
|
|
3 |
* $Source: /development/c/demos/varargs.c,v $
|
|
4 |
*
|
|
5 |
*/
|
|
6 |
|
|
7 |
#include <stdio.h>
|
|
8 |
#include <stdlib.h>
|
|
9 |
#include <stdarg.h>
|
|
10 |
|
|
11 |
int params (char *fmt, ...)
|
|
12 |
{
|
|
13 |
int i = 0;
|
|
14 |
va_list list;
|
|
15 |
char c;
|
|
16 |
char *s;
|
|
17 |
int value;
|
|
18 |
|
|
19 |
va_start (list, fmt);
|
|
20 |
|
|
21 |
while (*list) {
|
|
22 |
switch (*fmt++) {
|
|
23 |
case 'c':
|
|
24 |
c = va_arg (list, int);
|
|
25 |
|
|
26 |
printf (" Char: %c\n", c);
|
|
27 |
i++;
|
|
28 |
break;
|
|
29 |
case 's':
|
|
30 |
s = va_arg (list, char *);
|
|
31 |
|
|
32 |
printf ("String: %s\n", s);
|
|
33 |
i++;
|
|
34 |
break;
|
|
35 |
case 'd':
|
|
36 |
value = va_arg (list, int);
|
|
37 |
|
|
38 |
printf (" Value: %d\n", value);
|
|
39 |
i++;
|
|
40 |
break;
|
|
41 |
default:
|
|
42 |
printf ("Unknown format character\n");
|
|
43 |
}
|
|
44 |
}
|
|
45 |
|
|
46 |
va_end (list);
|
|
47 |
|
|
48 |
return i;
|
|
49 |
}
|
|
50 |
|
|
51 |
int main (int argc, char **argv)
|
|
52 |
{
|
|
53 |
|
|
54 |
printf ("Number of Parameter: %d\n", params ("Xcssssd", 'I', "made", "this", "application", "in", 2008, NULL));
|
|
55 |
|
|
56 |
return EXIT_SUCCESS;
|
|
57 |
}
|