954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

variable arguments in C

how can we pass variable arguments in any function of C?

hitesh_mathpal
Newbie Poster
4 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I think this should work, but i didn't test it, please do it yourself.

#include <stdarg.h>

void stringstofile (FILE *filehandle, ...)
{
	va_list vargs;

	va_start (vargs, filehandle);
	while (va_arg (vargs, char *))
		fputs (va_arg (vargs, char *), filehandle);
	va_end (vargs);
}
TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 

Google first....

ProgrammersTalk
Junior Poster in Training
84 posts since Jun 2007
Reputation Points: 21
Solved Threads: 7
 
how can we pass variable arguments in any function of C?

Simply to answer your question.

#include <stdio.h>

int sum;

int main() {

sum = myfunction(5, 10);

}

int myfunction(int argument1, int argument2) {

      return argument1 + argument2;

}


Code not tested, but see no reason why it shouldn't work. :)


Cheers.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 
Code not tested, but see no reason why it shouldn't work. :) Cheers.

Agree it should work but not the way the OP intended because it does not allow for variable number of arguments.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

1) What's the OP?

2) didn't want to over complicate things for the guy, as he seems to be just starting out in C, giving him a basic understanding of parameter passing would be more beneficial without talking to him about 'VA'.

That's my opinion anyway.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

1) What's the OP?

2) didn't want to over complicate things for the guy, as he seems to be just starting out in C, giving him a basic understanding of parameter passing would be more beneficial without talking to him about 'VA'.

That's my opinion anyway.

OP = Original Poster. I thought OP was a pretty common term, but maybe I was wrong. That's what I get for using an anronym.

He asked for variable arguments -- the code you posted did nothing to answer that question. I don't know his experience and neither does anyone else. He must have at least some programming knowledge in order to even ask the question.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

Dear friend , I am asking about a function like printf(). as it takes the variable argument.How can we define a function like this?

hitesh_mathpal
Newbie Poster
4 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

OP = Original Poster. I thought OP was a pretty common term, but maybe I was wrong. That's what I get for using an anronym.

He asked for variable arguments -- the code you posted did nothing to answer that question. I don't know his experience and neither does anyone else. He must have at least some programming knowledge in order to even ask the question.


Haha, i thought he ment passing variable arguments.. as you can pass an array as a parameter (using pointers of course), so i thought he ment how do you pass a variable to a function.

Oh well :)

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Haha, i thought he ment passing variable arguments.. as you can pass an array as a parameter (using pointers of course), so i thought he ment how do you pass a variable to a function.

Oh well :)

Oh, now I understand the reason for your post. :)Dear friend , I am asking about a function like printf(). as it takes the variable argument.How can we define a function like this?

Most functions that take a variable number of arguments are declared with at least one real argument followed by three periods, like the code posted by Narue and TkTkorrovi.

printf() does not use the periods because of theformat parameter which indicates the number of arguments and their data types.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

>Most functions that take a variable number of arguments are declared
>with at least one real argument followed by three periods
All of them, actually. :) va_start requires the last non-variadic parameter as its second argument, so at least one non-variadic parameter is required as well as the ellipsis to specify further variadic parameters.

>printf() does not use the periods because of the format parameter
>which indicates the number of arguments and their data types.
Since when? This is the prototype for printf, and I see an ellipsis:

int printf ( const char *format, ... );

The format string is for evaluating the variadic parameters, it's not a replacement for the required syntax.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Narue: thanks for the correction :icon_redface:

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

It's tested now

#include <stdio.h>
#include <stdarg.h>

void stringstofile (FILE *fp, ...)
{
        char *p;
        va_list ap;

        va_start (ap, fp);
        while ((p = va_arg (ap, char *)))
                fprintf (fp, "%s\n", p);
        va_end (ap);
}

int main ()
{
        stringstofile (stdout, "one",
                "two", "three", NULL);
        return 0;
}

and the output is

one
two
three
TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You