TUTORIAL: Pass Variable Number of Arguments to a C Function

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:

CPP Code: [ [Download ] [ [URL=“javascript:void(0)”]Select ] [ [URL=“javascript:void(0)”]Expand ] [URL=“javascript:void(0)”]Line Numbers Off ]
#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 &lt; 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

", total);

return 0;

}

[LIST=1]
[]#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
", total);
[
]
[] return 0;
[
]}
[/LIST]

How It Works

Let’s look at the code line by line:

CPP Code: [ [Download ] [URL=“javascript:void(0)”]Select ]
#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.

CPP Code: [ [Download ] [URL=“javascript:void(0)”]Select ]
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.

CPP Code: [ [Download ] [URL=“javascript:void(0)”]Select ]
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.

CPP Code: [ [Download ] [URL=“javascript:void(0)”]Select ]
va_start(args, num);

va_start is a macro used to initialize the argument list so that we can begin reading arguments from it.

CPP Code: [ [Download ] [ [URL=“javascript:void(0)”]Select ] [URL=“javascript:void(0)”]Line Numbers Off ]
int i, total = 0;
for( i = 0; i < num; ++i ) {
total += va_arg(args, int);
}

[LIST=1]
[]int i, total = 0;
[
]for( i = 0; i < num; ++i ) {
[] total += va_arg(args, int);
[
]}
[/LIST]

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.

CPP Code: [ [Download ] [URL=“javascript:void(0)”]Select ]
va_end(args);

va_end is another macro that cleans up our args object for us when we’re done using it.

CPP Code: [ [Download ] [URL=“javascript:void(0)”]Select ]
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!Attachments:
MultipleArgumentsC.zip (646 Bytes) Downloaded 45 times
Complete source code for the sum() example used in this tutorial.