1.What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void dumplist(int, ...);
int main()
{
dumplist(2, 4, 8);
dumplist(3, 6, 9, 7);
return 0;
}
void dumplist(int n, ...)
{
va_list p; int i;
va_start(p, n);
while(n-->0)
{
i = va_arg(p, int);
printf("%d", i);
}
va_end(p);
printf("\n");
}
Explanation:
In the list, the first parameter always represents the number of arguments to be passed.
So the 2 in the first function represents that there are 2 arguments.
And 3 in second function represents there are 3 arguments.
So first it prints 4 and 8.
Next, it will print 6, 9, 7. Excluding 4 and 8.
4 and 8 are actually to define a number of parameters. And they are not get printed.
2.Which of the following statements are FALSE about the below code?
int main(int ac, char *av[])
{
}
[A]. ac contains count of arguments supplied at command-line
[B]. av[] contains addresses of arguments supplied at a command line
[C]. In place of ac and av, argc and argv should be used.
[D]. The variables ac and av are always local to main()
Explanation:
Answer C is not correct for this question. According to Posix standard and other standards for declaring variable names, variable name can be any valid variable name. In this case we can use ac and av instead of argc and argv since these are only variable names for argument counter and argument vectors of command line argument.