Tuesday, July 28, 2009

How to use "optional parameter" in c language function?

void testfn(int a, ...) {


printf("a = %d\n", a);


/* how to access the rest? */


}

How to use "optional parameter" in c language function?
If you are trying to do a variable-argument-list, like printf does, see the link I have attached.





Basically, you just do:





void testfn(int a, ...){


va_list args;


va_start(args,a);


int x = va_arg(args, int);


va_end(args);


}





There are 'vprintf' and 'vsprintf' functions which take a va_list as parameter so you can pass on your multiple items to a printf function.


No comments:

Post a Comment