1 /* varargs.c -
2 * (Added as part of fix for bug 15306 - "call" to varargs functions fails)
3 * This program is intended to let me try out "call" to varargs functions
4 * with varying numbers of declared args and various argument types.
5 * - RT 9/27/95
6 */
7
8 #include <stdio.h>
9 #include <stdarg.h>
10
11 int find_max1(int, ...);
12 int find_max2(int, int, ...);
13 double find_max_double(int, double, ...);
14
15 char ch;
16 unsigned char uc;
17 short s;
18 unsigned short us;
19 int a,b,c,d;
20 int max_val;
21 long long ll;
22 float fa,fb,fc,fd;
23 double da,db,dc,dd;
24 double dmax_val;
25
main()26 int main() {
27 c = -1;
28 uc = 1;
29 s = -2;
30 us = 2;
31 a = 1;
32 b = 60;
33 max_val = find_max1(1, 60);
34 max_val = find_max1(a, b);
35 a = 3;
36 b = 1;
37 c = 4;
38 d = 2;
39 max_val = find_max1(3, 1, 4, 2);
40 max_val = find_max2(a, b, c, d);
41 da = 3.0;
42 db = 1.0;
43 dc = 4.0;
44 dd = 2.0;
45 dmax_val = find_max_double(3, 1.0, 4.0, 2.0);
46 dmax_val = find_max_double(a, db, dc, dd);
47
48 return 0;
49 }
50
51 /* Integer varargs, 1 declared arg */
52
find_max1(int num_vals,...)53 int find_max1(int num_vals, ...) {
54 int max_val = 0;
55 int x;
56 int i;
57 va_list argp;
58 va_start(argp, num_vals);
59 printf("find_max(%d,", num_vals);
60 for (i = 0; i < num_vals; i++) {
61 x = va_arg(argp, int);
62 if (max_val < x) max_val = x;
63 if (i < num_vals - 1)
64 printf(" %d,", x);
65 else
66 printf(" %d)", x);
67 }
68 printf(" returns %d\n", max_val);
69 return max_val;
70 }
71
72 /* Integer varargs, 2 declared args */
73
find_max2(int num_vals,int first_val,...)74 int find_max2(int num_vals, int first_val, ...) {
75 int max_val = 0;
76 int x;
77 int i;
78 va_list argp;
79 va_start(argp, first_val);
80 x = first_val;
81 if (max_val < x) max_val = x;
82 printf("find_max(%d, %d", num_vals, first_val);
83 for (i = 1; i < num_vals; i++) {
84 x = va_arg(argp, int);
85 if (max_val < x) max_val = x;
86 printf(", %d", x);
87 }
88 printf(") returns %d\n", max_val);
89 return max_val;
90 }
91
92 /* Double-float varargs, 2 declared args */
93
find_max_double(int num_vals,double first_val,...)94 double find_max_double(int num_vals, double first_val, ...) {
95 double max_val = 0;
96 double x;
97 int i;
98 va_list argp;
99 va_start(argp, first_val);
100 x = first_val;
101 if (max_val < x) max_val = x;
102 printf("find_max(%d, %f", num_vals, first_val);
103 for (i = 1; i < num_vals; i++) {
104 x = va_arg(argp, double);
105 if (max_val < x) max_val = x;
106 printf(", %f", x);
107 }
108 printf(") returns %f\n", max_val);
109 return max_val;
110 }
111
112