C Programming Questions and Answers – Variable Length Argument
C Programming Questions and Answers – Variable Length Argument
1.What is the output of this C code?
#include <stdio.h>
#include <stdarg.h>
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
View Answer
Answer:c
2.Which of the following function with ellipsis are illegal?
a) void func(…);
b) void func(int, …);
c) void func(int, int, …);
d) Both (a) and (c)
View Answer
Answer:a
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
View Answer
Answer:a
4.Which header file includes a function for variable number of arguments?
a) stdlib.h
b) stdarg.h
c) ctype.h
d) Both (a) and (b)
View Answer
Answer:b
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
View Answer
Answer:b
6.The type va_list is used in an argument list
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
View Answer
Answer:a
7.The declaration … can
a) Appear anywhere in the function declaration
b) Only appear at the end of an argument list.
c) Nothing
d) Both a & b
View Answer
Answer:b
8.Each call of va_arg
a) returns one argument
b) Steps va_list variable to the next
c) Both a & b
d) None of the mentioned
View Answer
Answer:c
9.The standard header _______ is used for variable list arguments (…) in C.
a) <stdio.h >
b) <stdlib.h>
c) <math.h>
d) <stdarg.h>
View Answer
Answer:d
10.va_end does whatever.
a) Cleanup is necessary
b) Bust be called before the program returns.
c) Both a &
d) None of the mentioned
View Answer
Answer:c
11.What is the output of this C code?
#include <stdio.h>
int f(char chr, …);
int main()
{
char c = 97;
f(c);
return 0;
}
int f(char c, …)
{
printf(“%c\n”, c);
}
a) Compile time error
b) Undefined behaviour
c) 97
d) a
View Answer
Answer:a
12.What is the output of this C code?
#include <stdio.h>
#include <stdarg.h>
int f(…);
int main()
{
char c = 97;
f(c);
return 0;
}
int f(…)
{
va_list li;
char c = va_arg(li, char);
printf(“%c\n”, c);
}
a) Compile time error
b) Undefined behaviour
c) 97
d) a
View Answer
Answer:a
13.What is the output of this C code?
#include <stdio.h>
#include <stdarg.h>
int f(char c, …);
int main()
{
char c = 97, d = 98;
f(c, d);
return 0;
}
int f(char c, …)
{
va_list li;
va_start(li, c);
char d = va_arg(li, char);
printf(“%c\n”, d);
va_end(li);
}
a) Compile time error
b) Undefined behaviour
c) a
d) b
View Answer
Answer:b
14.What is the output of this C code?
#include <stdio.h>
#include <stdarg.h>
int f(char c, …);
int main()
{
char c = 97, d = 98;
f(c, d);
return 0;
}
int f(char c, …)
{
va_list li;
va_start(li, c);
char d = va_arg(li, int);
printf(“%c\n”, d);
va_end(li);
}
a) Compile time error
b) Undefined behaviour
c) a
d) b
View Answer
Answer:d
15.What is the output of this C code?
#include <stdio.h>
#include <stdarg.h>
int f(int c, …);
int main()
{
int c = 97;
float d = 98;
f(c, d);
return 0;
}
int f(int c, …)
{
va_list li;
va_start(li, c);
float d = va_arg(li, float);
printf(“%f\n”, d);
va_end(li);
}
a) Compile time error
b) Undefined behaviour
c) 97.000000
d) 98.000000
View Answer
Answer:b