Variadic Functions

By Danny Kalev, ITworld |  How-to

Here's the complete emit() function:

void emit(const char *fmt,...)
{
va_list ap /*will point to each unnamed argument in turn*/
int num;
double d;
const char *p=fmt;
va_start(ap,fmt); /* point to first element after fmt*/
while(*p)
{
if (*p=='i') /*int*/
{
num=va_arg(ap,int);
printf("%d",num);
}
else if (*p=='d') /*double*/
{
d=va_arg(ap,double);
printf("%f",d);
}
else
{
printf("unsupported format flag");
break;
}
++p;
}/*while*/
va_end(ap) /*cleanup*/
}
int main()
{
double salary=1000.00;
int id=1234;
emit("di",salary, id);
}

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question
randomness