C-Variable Numbers and Arguments Questions and Answers
Practice ModeShowing 10 of 28 questions
Q1
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=10;
float f=3.14; char *p="Hello";
p1=fun1;
p2=fun2;
(*p1)(ch, i, &i, &f, p);
(*p2)(ch, i, &i, &f, p);
return 0;
}
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);
}
Answer: Option B
Q2
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");
}
Answer: Option C
Q3
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(1, "Apple", "Boys", "Cats", "Dogs");
fun2(2, 12, 13, 14);
return 0;
}
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);
}
Answer: Option D
Q4
What is the purpose of variable arguments (varargs) in C?
Answer: Option B
Explanation: Variable arguments allow functions to accept an indefinite number of arguments, providing flexibility when the exact number of arguments is unknown at compile time.
Q5
Which header file is required for using variable arguments in C?
Answer: Option B
Explanation: The stdarg.h header provides the necessary macros (va_list, va_start, va_arg, va_end) for implementing variable arguments.
Q6
What is the correct syntax for a function with variable arguments?
Answer: Option C
Explanation: The ellipsis (...) indicates variable arguments and must appear after at least one fixed parameter.
Q7
Which macro is used to initialize the argument pointer in variable functions?
Answer: Option C
Explanation: va_start initializes the argument pointer to the first variable argument.
Q8
What does the va_arg macro do?
Answer: Option C
Explanation: va_arg retrieves the next argument from the variable argument list and advances the pointer.
Q9
Which macro must be called before returning from a variable argument function?
Answer: Option D
Explanation: va_end performs necessary cleanup and must be called before the function returns.
Q10
What type is used to declare a variable for accessing variable arguments?
Answer: Option B
Explanation: va_list is the type used to declare a variable that will traverse the variable argument list.