Tsallaka zuwa abun ciki
Justice Tech School

Variadic Functions

<stdarg.h> lets a function accept a variable number of arguments, like printf. Use va_list, va_start, va_arg and va_end.

Example
#include <stdio.h>
#include <stdarg.h>
int sum(int n, ...) {
    va_list ap; va_start(ap, n);
    int t = 0;
    for (int i = 0; i < n; i++) t += va_arg(ap, int);
    va_end(ap);
    return t;
}
int main(void) { printf("%d\n", sum(4, 1, 2, 3, 4)); return 0; }

Result

10

Exercise

Read the next argument.

va_arg(ap, )

Quiz Test

Which header supports variadic functions?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.