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
26 #ifdef TEST_COMPLEX
27 extern float crealf (float _Complex);
28 extern double creal (double _Complex);
29 extern long double creall (long double _Complex);
30
31 float _Complex fc1 = 1.0F + 1.0iF;
32 float _Complex fc2 = 2.0F + 2.0iF;
33 float _Complex fc3 = 3.0F + 3.0iF;
34 float _Complex fc4 = 4.0F + 4.0iF;
35
36 double _Complex dc1 = 1.0 + 1.0i;
37 double _Complex dc2 = 2.0 + 2.0i;
38 double _Complex dc3 = 3.0 + 3.0i;
39 double _Complex dc4 = 4.0 + 4.0i;
40
41 long double _Complex ldc1 = 1.0L + 1.0Li;
42 long double _Complex ldc2 = 2.0L + 2.0Li;
43 long double _Complex ldc3 = 3.0L + 3.0Li;
44 long double _Complex ldc4 = 4.0L + 4.0Li;
45
46 #endif
47
main()48 int main() {
49 c = -1;
50 uc = 1;
51 s = -2;
52 us = 2;
53 a = 1;
54 b = 60;
55 max_val = find_max1(1, 60);
56 max_val = find_max1(a, b);
57 a = 3;
58 b = 1;
59 c = 4;
60 d = 2;
61 max_val = find_max1(3, 1, 4, 2);
62 max_val = find_max2(a, b, c, d);
63 da = 3.0;
64 db = 1.0;
65 dc = 4.0;
66 dd = 2.0;
67 dmax_val = find_max_double(3, 1.0, 4.0, 2.0);
68 dmax_val = find_max_double(a, db, dc, dd);
69
70 return 0;
71 }
72
73 /* Integer varargs, 1 declared arg */
74
find_max1(int num_vals,...)75 int find_max1(int num_vals, ...) {
76 int max_val = 0;
77 int x;
78 int i;
79 va_list argp;
80 va_start(argp, num_vals);
81 printf("find_max(%d,", num_vals);
82 for (i = 0; i < num_vals; i++) {
83 x = va_arg(argp, int);
84 if (max_val < x) max_val = x;
85 if (i < num_vals - 1)
86 printf(" %d,", x);
87 else
88 printf(" %d)", x);
89 }
90 printf(" returns %d\n", max_val);
91 return max_val;
92 }
93
94 /* Integer varargs, 2 declared args */
95
find_max2(int num_vals,int first_val,...)96 int find_max2(int num_vals, int first_val, ...) {
97 int max_val = 0;
98 int x;
99 int i;
100 va_list argp;
101 va_start(argp, first_val);
102 x = first_val;
103 if (max_val < x) max_val = x;
104 printf("find_max(%d, %d", num_vals, first_val);
105 for (i = 1; i < num_vals; i++) {
106 x = va_arg(argp, int);
107 if (max_val < x) max_val = x;
108 printf(", %d", x);
109 }
110 printf(") returns %d\n", max_val);
111 return max_val;
112 }
113
114 /* Double-float varargs, 2 declared args */
115
find_max_double(int num_vals,double first_val,...)116 double find_max_double(int num_vals, double first_val, ...) {
117 double max_val = 0;
118 double x;
119 int i;
120 va_list argp;
121 va_start(argp, first_val);
122 x = first_val;
123 if (max_val < x) max_val = x;
124 printf("find_max(%d, %f", num_vals, first_val);
125 for (i = 1; i < num_vals; i++) {
126 x = va_arg(argp, double);
127 if (max_val < x) max_val = x;
128 printf(", %f", x);
129 }
130 printf(") returns %f\n", max_val);
131 return max_val;
132 }
133
134
135 #ifdef TEST_COMPLEX
136 float _Complex
find_max_float_real(int num_vals,...)137 find_max_float_real (int num_vals, ...)
138 {
139 float _Complex max = 0.0F + 0.0iF;
140 float _Complex x;
141 va_list argp;
142 int i;
143
144 va_start(argp, num_vals);
145 for (i = 0; i < num_vals; i++)
146 {
147 x = va_arg (argp, float _Complex);
148 if (crealf (max) < crealf (x)) max = x;
149 }
150
151 return max;
152 }
153
154 double _Complex
find_max_double_real(int num_vals,...)155 find_max_double_real (int num_vals, ...)
156 {
157 double _Complex max = 0.0 + 0.0i;
158 double _Complex x;
159 va_list argp;
160 int i;
161
162 va_start(argp, num_vals);
163 for (i = 0; i < num_vals; i++)
164 {
165 x = va_arg (argp, double _Complex);
166 if (creal (max) < creal (x)) max = x;
167 }
168
169 return max;
170 }
171
172 long double _Complex
find_max_long_double_real(int num_vals,...)173 find_max_long_double_real (int num_vals, ...)
174 {
175 long double _Complex max = 0.0L + 0.0iL;
176 long double _Complex x;
177 va_list argp;
178 int i;
179
180 va_start(argp, num_vals);
181 for (i = 0; i < num_vals; i++)
182 {
183 x = va_arg (argp, long double _Complex);
184 if (creall (max) < creal (x)) max = x;
185 }
186
187 return max;
188 }
189
190
191 #endif /* TEST_COMPLEX */
192