C-Variable Numbers and Arguments Questions and Answers

Practice Mode
Showing 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); }
  • A A 10 3.14 A 10 3.14
  • B A 10 10 3.140000 Hello A 10 10 3.140000 Hello
  • C A 10 Hello A 10 Hello
  • D Error
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"); }
  • A 2 4 3 6
  • B 2 4 8 3, 6, 9, 7
  • C 4 8 6 9 7
  • D 1 1 1 1 1 1 1
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); }
  • A Dogs 12
  • B Cats 14
  • C Boys 13
  • D Apple 12
Answer: Option D
Q4
What is the purpose of variable arguments (varargs) in C?
  • A To allow functions to accept a fixed number of arguments
  • B To enable functions to accept variable number of arguments
  • C To declare variables with dynamic types
  • D To create variable-sized arrays
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?
  • A stdlib.h
  • B stdarg.h
  • C varargs.h
  • D stdio.h
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?
  • A void func(...)
  • B void func(int args...)
  • C void func(int count, ...)
  • D void func(...int args)
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?
  • A va_init
  • B va_begin
  • C va_start
  • D va_list
Answer: Option C
Explanation: va_start initializes the argument pointer to the first variable argument.
Q8
What does the va_arg macro do?
  • A Starts the argument list
  • B Ends the argument list
  • C Retrieves the next argument
  • D Counts the number of arguments
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?
  • A va_start
  • B va_clean
  • C va_finish
  • D va_end
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?
  • A va_args
  • B va_list
  • C arg_list
  • D variable_args
Answer: Option B
Explanation: va_list is the type used to declare a variable that will traverse the variable argument list.
Questions and Answers for Competitive Exams Various Entrance Test