-
Notifications
You must be signed in to change notification settings - Fork 222
/
variadic-functions-in-c.c
51 lines (36 loc) · 1.17 KB
/
variadic-functions-in-c.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
43
44
45
46
47
48
49
50
51
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) < (b) ? (b) : (a))
int sum(int count, ...) {
va_list ap;
int sum;
va_start (ap, count); /* Initialize the argument list. */
sum = 0;
for (int i = 0; i < count; i++)
sum += va_arg (ap, int); /* Get the next argument value. */
va_end (ap); /* Clean up. */
return sum;
}
int min(int count, ...) {
va_list ap;
int minimum = MAX_ELEMENT;
va_start (ap, count); /* Initialize the argument list. */
for (int i = 0; i < count; i++) {
int next_el;
next_el = va_arg (ap, int);
minimum = _min(minimum, next_el);
}
va_end (ap); /* Clean up. */
return minimum;
}
int max(int count, ...) {
va_list ap;
int maximum = MIN_ELEMENT;
va_start (ap, count); /* Initialize the argument list. */
for (int i = 0; i < count; i++) {
int next_el;
next_el = va_arg (ap, int);
maximum = _max(maximum, next_el);
}
va_end (ap); /* Clean up. */
return maximum;
}