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

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);
}

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.

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.

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.

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.

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

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 :)

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 the format parameter which indicates the number of arguments and their data types.

>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: thanks for the correction :icon_redface:

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.