# TUTORIAL: Pass Variable Number of Arguments to a C Function

**URL:** https://forum.kirupa.com/t/tutorial-pass-variable-number-of-arguments-to-a-c-function/301121
**Category:** Uncategorized
**Created:** [December 3, 2009, 7:10pm UTC](https://forum.kirupa.com/t/tutorial-pass-variable-number-of-arguments-to-a-c-function/301121 "2009-12-03T19:10:39Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![rockguy](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@rockguy](https://forum.kirupa.com/u/rockguy)
#### Post date: [December 3, 2009, 7:10pm UTC](https://forum.kirupa.com/t/tutorial-pass-variable-number-of-arguments-to-a-c-function/301121/1 "2009-12-03T19:10:39Z")

</div>

**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](http://www.ozzu.com/php-tutorials/tutorial-pass-variable-num-arguments-php-function-t96484.html)._

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](http://www.ozzu.com/bbc_download.php?p=541216) ]  
#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](http://www.ozzu.com/bbc_download.php?p=541216&item=1) ]  
#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](http://www.ozzu.com/bbc_download.php?p=541216&item=2) ]  
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](http://www.ozzu.com/bbc_download.php?p=541216&item=3) ]  
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](http://www.ozzu.com/bbc_download.php?p=541216&item=4) ]  
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](http://www.ozzu.com/bbc_download.php?p=541216&item=5) ]  
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](http://www.ozzu.com/bbc_download.php?p=541216&item=6) ]  
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](http://www.ozzu.com/bbc_download.php?p=541216&item=7) ]  
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](http://www.ozzu.com/cpp-tutorials/tutorial-writing-custom-printf-wrapper-function-t89166.html).

I always welcome questions or feedback about this tutorial. Simply post a reply or PM me; I’m glad to help!Attachments:  
 ![](http://www.ozzu.com/styles/light/imageset/icon_topic_attach.gif) [MultipleArgumentsC.zip](http://www.ozzu.com/download/file.php?id=713) (646 Bytes) Downloaded 45 times  
_Complete source code for the sum() example used in this tutorial._
