Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Variable Number of Arguments Questions
C varargs diagnostics: point out the specific error(s) in this program using
. Rewrite focuses on why ellipsis functions need a named parameter and a correct va_start anchor. #include
#include
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); }
C varargs with default promotions: identify the error when passing floating values through an ellipsis and retrieving them with va_arg. #include
#include
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); } }
C varargs correctness: spot the error involving parameter naming, shadowing, and va_start anchor in this display function. #include
#include
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); }
C varargs under Turbo C note: does this program compile and what does it print when characters are passed through an ellipsis? #include
#include
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); } }
C varargs API usage: identify the error when va_list is used without calling va_start in a variable-argument function. #include
#include
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); }
C varargs across functions: identify the incorrect usage when forwarding varargs and misusing va_start. #include
#include
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); }
C varargs typing: identify the primary error when the va_list variable is declared with the wrong type and used with va_start. #include
#include
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); }
C varargs theory check: the macro va_start initializes a pointer to the beginning of the list of fixed (named) arguments — True or False?
C varargs usage check: should a variadic function use va_arg() specifically to extract the last argument in the variable list — True or False?
C varargs fundamentals: is va_list an array type that “holds information needed by va_arg and va_end” — True or False?
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.
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()?
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?
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?
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?
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)?
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?
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.)
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?
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?
1
2