Introduction
Note: The complete source code for the example in this tutorial can be downloaded at the end of the tutorial.
Note: Trying to do this in PHP? Check this out.
When declaring a function, the type and number of arguments are usually fixed at compile time. But sometimes it is necessary for a function to be able to accept a variable number of arguments unknown until run-time.
This tutorial will show you how to create a C function that can accept a variable number of arguments.
In A Nutshell
If you're simply looking for an example to go off of, I won't keep you waiting. Here's an example of a function sum() that adds up all of the integers passed to it and returns the sum:
#include <stdarg.h>
#include <stdio.h>
#include <stdarg.h>
int sum( int num, ... ) {
va_list args;
va_start(args, num);
int i, total = 0;
for( i = 0; i < num; ++i ) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
int main() {
int total = sum(5, 1, 2, 3, 4, 5);
printf("Total: %d\n", total);
return 0;
}
How It Works
Let's look at the code line by line:
#include <stdarg.h>
The stdarg library provides us with the va_list data type, as well as the macros va_start, va_arg, and va_end for manipulating the list of arguments.
int sum( int num, ... ) {
To declare the function, we use a triple dot (...) to signify that the function accepts a variable number of arguments. The first argument, num, is used to indicate how many arguments were passed into the function.
If we didn't want to use the first argument as a argument length specifier, we could have simply used the first argument as part of the numbered to be summed up, and then used some sort of special number (negative one, perhaps) as an indicator that the last argument has been reached.
va_list args;
va_list is a data type used by stdarg to hold the list of arguments passed into the function. Here we declare a variable called args.
va_start(args, num);
va_start is a macro used to initialize the argument list so that we can begin reading arguments from it.
int i, total = 0;
for( i = 0; i < num; ++i ) {
total += va_arg(args, int);
}
Here, we loop through the list of arguments until we've read as many as specified in our first argument. va_arg is the macro used to read an argument from the list. It takes two parameters: the va_list object we created, args, and a data type. va_arg will return the next argument as this type.
va_end(args);
va_end is another macro that cleans up our args object for us when we're done using it.
return total;
Finally, we return the total we calculated in the function.
Conclusion
You should now know how to create a C function that accepts a variable number of arguments at runtime. This technique is particularly useful when creating wrapper functions for functions that already accept a variable number of arguments, such as printf(). This is demonstrated in my other tutorial, Writing a Custom printf() Wrapper Function in C.
I always welcome questions or feedback about this tutorial. Simply post a reply or PM me; I'm glad to help! Full code:
sum.c
/*
* A function that accepts a variable number of
* arguments at runtime.
*
* @author Jeff Linse
* Created for the tutorial found at
* https://www.ozzu.com/wiki/tagged/c++
*
* To compile with gcc:
* gcc -o sum sum.c
*
* To run:
* ./sum
*/
// included for standard output
#include <stdio.h>
// included to allow variable-length arguments
#include <stdarg.h>
// the first argument specified the number of
// arguments to follow
int sum( int num, ... ) {
// declare the argument list
va_list args;
// initialize the argument list
va_start(args, num);
// loop through and add all arguments
int i, total = 0;
for( i = 0; i < num; ++i ) {
// va_returns the next argument from the
// list as the type specified (i.e. int)
total += va_arg(args, int);
}
// clean up memory used by the argument list
va_end(args);
// return the total we calculated
return total;
}
int main() {
// sum up some integers
int total = sum(5, 1, 2, 3, 4, 5);
printf("Total: %d\n", total);
return 0;
}
This page was published on It was last revised on