Friday, February 8, 2008

What is Ellipsis or … in c ?

Ans: Ellipsis is there consecutive period (.) with no white space. With the help of ellipsis we can write a function of variable number of parameter.
Example:
int number(int a,...)
{
return a;
}
void main() {
int i;
i=number(5,4,3,2,1);
clrscr();
printf("%d",i);
getch();
}

Output: 5
In header file stdarg.h has three macro va_arg,va_end,va_start ,with help of this macro we can know other parameter of a function of variable number of parameter.
Syntax:
void va_start (va_list ap,lastfix)
type va_arg(va_list ap,type)
void va_end(va_list ap);
va_list is data .
lastfix is a last fixed parameter used in a function of variable parameter.
type is used to know which data type are using in function of variable parameter .
va_arg always return next parameter from right to left.
va_start set the pointer ap to the first parameter from right side of a function of variable parameter.
va_end help in the normal return.
Example.
#include
#include
void sum(char *msg, ...)
{
int total = 0;
va_list p;
int arg;
va_start(p, msg);
while ((arg = va_arg(p,int)) != 0) {
total += arg;
}
printf(msg, total);
va_end(p);
}

void main() {
sum("The total sum is %d\n", 5,7,11,8);
getch();
}
Output: 31

0 comment here::