Here is a listing of C programming questions on “Variable Length Argument” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
#include
-
void func(int, ...);
-
int main()
-
{
-
func(2, 3, 5, 7, 11, 13);
-
return 0;
-
}
-
void func(int n, ...)
-
{
-
int number, i = 0;
-
va_list start;
-
va_start(start, n);
-
while (i != 3)
-
{
-
number = va_arg(start, int);
-
i++;
-
}
-
printf("%d", number);
-
}
a) 3
b) 5
c) 7
d) 11
Answer: c
Clarification: None.
2. Which of the following function with ellipsis are illegal?
a) void func(…);
b) void func(int, …);
c) void func(int, int, …);
d) none of the mentioned
Answer: a
Clarification: None.
3. Which of the following data-types are promoted when used as a parameter for an ellipsis?
a) char
b) short
c) int
d) none of the mentioned
Answer: a
Clarification: None.
4. Which header file includes a function for variable number of arguments?
a) stdlib.h
b) stdarg.h
c) ctype.h
d) both stdlib.h and stdarg.h
Answer: b
Clarification: None.
5. Which of the following macro extracts an argument from the variable argument list (ie ellipsis) and advance the pointer to the next argument?
a) va_list
b) va_arg
c) va_end
d) va_start
Answer: b
Clarification: None.
6. The type va_list in an argument list is used ________
a) To declare a variable that will refer to each argument in turn;
b) For cleanup
c) To create a list
d) There is no such type
Answer: a
Clarification: None.
7. In a variable length argument function, the declaration “…” can _______
a) Appear anywhere in the function declaration
b) Only appear at the end of an argument list
c) Nothing
d) None of the mentioned
Answer: b
Clarification: None.
8. Each call of va_arg _______
a) Returns one argument
b) Steps va_list variable to the next
c) Returns one argument & Steps va_list variable to the next
d) None of the mentioned
Answer: c
Clarification: None.