Variable Number of Arguments Questions

Practice Variable Number of Arguments MCQs with answers and explanations. Page 1 of 2.

Category
C Programming
Topic
Variable Number of Arguments
Page
1 / 2
Mode
Practice

Questions

Open any question to view the answer and explanation.

C varargs diagnostics: point out the specific error(s) in this program using <stdarg.h>. Rewrite focuses on why ellipsis functions need a named parameter and a correct va_start anchor. #include <stdio.h> #include <stdarg.h> fun(...); int main() { fun(3, 7, -11.2, 0.66); return 0; } fun(...) { va_list ptr; int num; va_start(ptr, n); /* 'n' is not a named parameter here */ num = va_arg(ptr, int); printf("%d", num); }
Open
View answer
C varargs with default promotions: identify the error when passing floating values through an ellipsis and retrieving them with va_arg. #include <stdio.h> #include <stdarg.h> int main() { void display(int num, ...); display(4, 12.5, 13.5, 14.5, 44.3); return 0; } void display(int num, ...) { float c; int j; va_list ptr; va_start(ptr, num); for (j = 1; j <= num; j++) { c = va_arg(ptr, float); /* incorrect type for promoted argument */ printf("%f", c); } }
Open
View answer
C varargs correctness: spot the error involving parameter naming, shadowing, and va_start anchor in this display function. #include <stdio.h> #include <stdarg.h> int main() { void display(char *s, int num1, int num2, ...); display("Hello", 4, 2, 12.5, 13.5, 14.5, 44.0); return 0; } void display(char s, int num1, int num2, ...) { double c; char s; / shadows parameter and has wrong type / va_list ptr; va_start(ptr, s); / wrong anchor: must be last named parameter (num2) */ c = va_arg(ptr, double); printf("%f", c); }
Open
View answer
C varargs under Turbo C note: does this program compile and what does it print when characters are passed through an ellipsis? #include <stdio.h> #include <stdarg.h> void display(int num, ...); int main() { display(4, 'A', 'a', 'b', 'c'); return 0; } void display(int num, ...) { char c; int j; va_list ptr; va_start(ptr, num); for (j = 1; j <= num; j++) { c = va_arg(ptr, char); /* relies on Turbo C behavior */ printf("%c", c); } }
Open
View answer
C varargs API usage: identify the error when va_list is used without calling va_start in a variable-argument function. #include <stdio.h> #include <stdarg.h> void varfun(int n, ...); int main() { varfun(3, 7, -11, 0); return 0; } void varfun(int n, ...) { va_list ptr; int num; num = va_arg(ptr, int); /* va_start not called */ printf("%d", num); }
Open
View answer
C varargs across functions: identify the incorrect usage when forwarding varargs and misusing va_start. #include <stdio.h> #include <stdarg.h> void display(char *s, ...); void show(char *t, ...); int main() { display("Hello", 4, 12, 13, 14, 44); return 0; } void display(char s, ...) { show(s, ...); / attempt to forward ellipsis directly */ } void show(char t, ...) { int a; va_list ptr; va_start(ptr, s); / wrong anchor, should be 't' */ a = va_arg(ptr, int); printf("%f", a); }
Open
View answer
C varargs typing: identify the primary error when the va_list variable is declared with the wrong type and used with va_start. #include <stdio.h> #include <stdarg.h> void varfun(int n, ...); int main() { varfun(3, 7, -11.2, 0.66); return 0; } void varfun(int n, ...) { float ptr; / WRONG: should be va_list / int num; va_start(ptr, n); / invalid: ptr is not a va_list */ num = va_arg(ptr, int); printf("%d", num); }
Open
View answer
C varargs theory check: the macro va_start initializes a pointer to the beginning of the list of fixed (named) arguments — True or False?
Open
View answer
C varargs usage check: should a variadic function use va_arg() specifically to extract the last argument in the variable list — True or False?
Open
View answer
C varargs fundamentals: is va_list an array type that “holds information needed by va_arg and va_end” — True or False?
Open
View answer
In C programming (stdarg.h), what does the macro va_arg do when iterating through a variable argument list? Clarify whether it both extracts the current argument value of the specified promoted type and advances the internal va_list position to the next argument.
Open
View answer
For a C function that receives a variable number of arguments (declared with an ellipsis ...), should the function use va_arg() to sequentially extract each unnamed argument from the va_list after calling va_start()?
Open
View answer
In C variadic functions, must there be at least one fixed (named) parameter before the ellipsis so that va_start() can be given a valid last named argument?
Open
View answer
In a C function that accepts a variable number of arguments, may the fixed (named) parameters appear after the ellipsis, or must the ellipsis be last in the parameter list?
Open
View answer
In a C variadic function, if va_start() has been called on a va_list, is it required to call va_end() before the function returns?
Open
View answer
Can you construct and pass a variable argument list to a variadic function at run time in standard C, or must you instead use a va_list and call a corresponding v* function (such as vprintf)?
Open
View answer
When defining a C function that accepts a variable number of arguments, should you include the ellipsis (...) in the prototype/declaration, or may you drop it?
Open
View answer
In C, can a function that accepts a variable argument list forward that same list to another function? (For example, wrap printf by calling vprintf with the captured va_list.)
Open
View answer
In a function that accepts a variable argument list, may the fixed (named) parameters appear at the end of the parameter list, or must they precede the ellipsis?
Open
View answer
In standard C, is it required that a function which accepts a variable argument list has at least one fixed (named) parameter before the ellipsis so that va_start() can be used correctly?
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.