Public Group

# Variable number of function args in C

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! Does any of you know how to make a fuction in C that accepts variable number of argumets? I googled a bit but haven't found anything interesting. Or do I have solve this using overriding the function, like void func(arg1); void func(arg1, arg2); etc.? Thanks.

##### Share on other sites
Google for va_start, va_arg, and va_end.

Briefly, you define the function like so: void func(arg1, ...). Note the ..., that means you can supply any number of arguments after arg1. Inside the function you use the va_ functions to access the variable arguments.

##### Share on other sites
Ok, thanks. I'm going to have a look at it.

##### Share on other sites
Ok, I found some documetation using linux man and found this piece of code:
#include <stdio.h>#include <stdarg.h>              void foo(char *fmt, ...) {                   va_list ap;                   int d;                   char c, *s;                   va_start(ap, fmt);                   while (*fmt)                        switch(*fmt++) {                        case 's':           /* string */                             s = va_arg(ap, char *);                             printf("string %s\n", s);                             break;                        case 'd':           /* int */                             d = va_arg(ap, int);                             printf("int %d\n", d);                             break;                        case 'c':           /* char */                             /* need a cast here since va_arg only                                takes fully promoted types */                             c = (char) va_arg(ap, int);                             printf("char %c\n", c);                             break;                        }                   va_end(ap);              }int main() {  foo("ce", "sls", "d32");  return 0;}

But I don't have a clue if I use the function correctly. Is there sth wrong? Because this only prints out "char" and that's all.

##### Share on other sites
It looks all right to me, but I just glanced over it. I just wanted to point out that C doesn't support overriding functions.

##### Share on other sites
At a glance, I'm thinking that what the function is expecting is something like:
foo("csd", 'e', "sls", 32);
The first parameter specifies two things: 1) how many further parameters there are (based on the number of characters), and 2) what the type of each of those parameters are. So "csd" is saying that there are three further parameters, the first is a character, the second is a string (character pointer), and the third is an integer.

##### Share on other sites
A correct use of your function would be: foo("cds", 'a', 121232, "test");

EDIT: too late.

##### Share on other sites
Oh yeah. Now I get it. Thanks a lot guys, you were a great help!

Edit: One more thing: C doesn't support fucntion overriding???

##### Share on other sites
here you go...

#include <stdio.h>#include <stdarg.h>void output(const char *fmt, ...){   // buffer, going to be used much like sprintf was   char buffer[80];   // arg list   va_list vl;   // start the list   va_start(vl,fmt);   // format all the stuff and put in in the buffer   vsprintf(buffer, fmt,vl);   // end the list   va_end(vl);   // yay! print our formatted stuff   printf("%s\n", buffer);}int main(void){   int cards = 81;   // useless code just made to show off the power of our new function!   for (int i = 10; i > 0; i--)      output("There are %d Cards in the %s! And %d divided by %f is%f", cards, "deck", cards, 2.3, cards--/2.3);   return 0;}

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 10
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632950
• Total Posts
3009374
• ### Who's Online (See full list)

There are no registered users currently online

×