C-Variable Numbers and Arguments Questions and Answers
C Variable Numbers and Arguments questions with answers help developers understand how functions in C can accept a varying number of parameters. Using standard macros like va_start, va_arg, and va_end, C programmers can design flexible functions similar to printf(). These programming questions and answers guide learners through the core logic and syntax used in handling variable arguments effectively. Such concepts are often asked in programming interview questions with answers during technical rounds in companies like Infosys, Tech Mahindra, and Wipro. Practicing C programming MCQs related to variable arguments helps build confidence in function handling and efficient coding techniques.
C-Variable Numbers and Arguments
Showing 10 of
28 questions
1. 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 10 3.14 A 10 3.14
- A 10 10 3.140000 Hello A 10 10 3.140000 Hello
- A 10 Hello A 10 Hello
- Error
2. 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"); }
- 2 4 3 6
- 2 4 8 3, 6, 9, 7
- 4 8 6 9 7
- 1 1 1 1 1 1 1
3. 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); }
- Dogs 12
- Cats 14
- Boys 13
- Apple 12
4. What is the purpose of variable arguments (varargs) in C?
- To allow functions to accept a fixed number of arguments
- To enable functions to accept variable number of arguments
- To declare variables with dynamic types
- To create variable-sized arrays
5. Which header file is required for using variable arguments in C?
- stdlib.h
- stdarg.h
- varargs.h
- stdio.h
6. What is the correct syntax for a function with variable arguments?
- void func(...)
- void func(int args...)
- void func(int count, ...)
- void func(...int args)
7. Which macro is used to initialize the argument pointer in variable functions?
- va_init
- va_begin
- va_start
- va_list
8. What does the va_arg macro do?
- Starts the argument list
- Ends the argument list
- Retrieves the next argument
- Counts the number of arguments
9. Which macro must be called before returning from a variable argument function?
- va_start
- va_clean
- va_finish
- va_end
10. What type is used to declare a variable for accessing variable arguments?
- va_args
- va_list
- arg_list
- variable_args