C
Variadic patterns
Learn variadic function patterns.
Variadic patterns
A common variadic pattern: use the first argument as the seed, then process the rest in a loop. This is used in max, min or avg functions.
For max_n: after va_start, read the first va_arg into int max. Then in for (int i = 1; i < count; i++) for each new v = va_arg(args, int) do if (v > max) max = v;. The loop starts at 1 because we already consumed the 0th element. This pattern is the variadic variant of the accumulator pattern. In sum_n we started with total = 0; in max_n we start with the first argument because the max of an empty set is undefined.
The max_n implementation
Let's write a full max_n implementation. The first argument goes into max, then a loop compares each new value.
Definition: int max_n(int count, ...) { va_list args; va_start(args, count); int max = va_arg(args, int); for (int i = 1; i < count; i++) { int v = va_arg(args, int); if (v > max) max = v; } va_end(args); return max; }. The loop starts at 1 because the 0th argument is already in max. Each step reads v and updates max if v is larger. Call: max_n(4, 5, 12, 8, 3) → 12. This pattern works for min_n too by changing > to <. Flexibility is the power of variable arguments.
Exercise The max_n function
Write int max_n(int count, ...). va_list args; va_start(args, count);. Start with int max = va_arg(args, int);. In for (int i = 1; i < count; i++) do int v = va_arg(args, int); and if (v > max) max = v;. va_end(args); return max;. In main call printf("%d\n", max_n(4, 5, 12, 8, 3));.
Show example solution
#include <stdio.h>
#include <stdarg.h>
int max_n(int count, ...) {
va_list args;
va_start(args, count);
int max = va_arg(args, int);
for (int i = 1; i < count; i++) {
int v = va_arg(args, int);
if (v > max) max = v;
}
va_end(args);
return max;
}
int main(void) {
printf("%d\n", max_n(4, 5, 12, 8, 3));
return 0;
}
Test yourself
-
Seed with the first argument
int max = va_____(args, int);
Show answer
int max = va_arg(args, int);
-
Accumulator patterns
- sum_n
- start with 0
- max_n
- start with first arg
- print_numbers
- print directly
- log_msg
- via vprintf
-
Why does the loop in max_n start at 1?
- the first arg was already read
- va_arg can't start at 0
- count must be greater than 1
- that's how for loops work
Show answer
the first arg was already read — The 0th arg was already assigned to max; the loop starts at 1.
Next lesson: Summary - Variable arguments