It is necessary to call the macro va_end if va_start is called in the function.
va_list is an array that holds information needed by va_arg and va_end
The macro va_arg is used to extract an argument from the variable argument list and advance the pointer to the next argument.
Can we pass a variable argument list to a function at run-time?
The macro va_arg is used to extract an argument from the fixed micro argument list and advance the pointer to the next argument.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void fun(char *msg, ...);
int main()
{
fun("IndiaBIX",0);
return}
void fun(char *msg, ...)
{
va_list ptr;
int num;
va_start(ptr, msg);
num = va_arg(ptr, int);
num = va_arg(ptr, int);
printf("%d", num);
}
The macro va_start is used to initialise a pointer to the beginning of the list of fixed arguments.
In a function that receives variable number of arguments the fixed arguments passed to the function can be at the end of argument list.
A function that receives variable number of arguments should use va_arg() to extract arguments from the variable argument list.
While defining a variable argument list function we drop the ellipsis(...)?
Is it necessary that in a function which accepts variable argument list there should be at least be one fixed argument?
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void fun1(char, int, int *, float *, char *);
void fun2(char ch, ...);
void (*p1)(char, int, int *, float *, char *);
void (*p2)(char ch, ...);
int main()
{
char ch='A'; int i= float f=3.char *p="Hello";
p1=fun p2=fun (*p1)(ch, i, &i, &f, p);
(*p2)(ch, i, &i, &f, p);
return}
void fun1(char ch, int i, int *pi, float *pf, char *p)
{
printf("%c %d %d %f %s \n", ch, i, *pi, *pf, p);
}
void fun2(char ch, ...)
{
int i, *pi; float *pf; char *p;
va_list list;
printf("%c ", ch);
va_start(list, ch);
i = va_arg(list, int);
printf("%d ", i);
pi = va_arg(list, int*);
printf("%d ", *pi);
pf = va_arg(list, float*);
printf("%f ", *pf);
p = va_arg(list, char *);
printf("%s", p);
}
For a function receives variable number of arguments it is necessary that the function should receive at least one fixed argument.
Can we write a function that takes a variable argument list and passes the list to another function?
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void dumplist(int, ...);
int main()
{
dumplist(8);
dumplist(7);
return}
void dumplist(int n, ...)
{
va_list p; int i;
va_start(p, n);
while(n--> {
i = va_arg(p, int);
printf("%d", i);
}
va_end(p);
printf("\n");
}
A function that receives variable number of arguments should use va_arg() to extract the last argument from the variable argument list.
Can the fixed arguments passed to the function that accepts variable argument list, occur at the end?
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void display(int num, ...);
int main()
{
display('A', 'B', 'C', 'D');
return}
void display(int num, ...)
{
char c, cint j;
va_list ptr, ptr va_start(ptr, num);
va_start(ptrnum);
for(j=j<=num; j++)
{
c = va_arg(ptr, int);
printf("%c", c);
c1 = va_arg(ptrint);
printf("%d\n", c1);
}
}
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void fun1(int num, ...);
void fun2(int num, ...);
int main()
{
fun1("Apple", "Boys", "Cats", "Dogs");
fun2(14);
return}
void fun1(int num, ...)
{
char *str;
va_list ptr;
va_start(ptr, num);
str = va_arg(ptr, char *);
printf("%s ", str);
}
void fun2(int num, ...)
{
va_list ptr;
va_start(ptr, num);
num = va_arg(ptr, int);
printf("%d", num);
}
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void display(char *s, ...);
void show(char *t, ...);
int main()
{
display("Hello",44);
return}
void display(char *s, ...)
{
show(s, ...);
}
void show(char *t, ...)
{
int a;
va_list ptr;
va_start(ptr, s);
a = va_arg(ptr, int);
printf("%f", a);
}