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.\n\n#include
\n#include
\nfun(...);\n\nint main()\n{\n fun(3, 7, -11.2, 0.66);\n return 0;\n}\nfun(...)\n{\n va_list ptr;\n int num;\n va_start(ptr, n); /* 'n' is not a named parameter here */\n num = va_arg(ptr, int);\n printf("%d", num);\n}
C varargs with default promotions: identify the error when passing floating values through an ellipsis and retrieving them with va_arg.\n\n#include
\n#include
\n\nint main()\n{\n void display(int num, ...);\n display(4, 12.5, 13.5, 14.5, 44.3);\n return 0;\n}\nvoid display(int num, ...)\n{\n float c; int j;\n va_list ptr;\n va_start(ptr, num);\n for (j = 1; j <= num; j++)\n {\n c = va_arg(ptr, float); /* incorrect type for promoted argument */\n printf("%f", c);\n }\n}
C varargs correctness: spot the error involving parameter naming, shadowing, and va_start anchor in this display function.\n\n#include
\n#include
\n\nint main()\n{\n void display(char *s, int num1, int num2, ...);\n display("Hello", 4, 2, 12.5, 13.5, 14.5, 44.0);\n return 0;\n}\nvoid display(char s, int num1, int num2, ...)\n{\n double c;\n char s; / shadows parameter and has wrong type /\n va_list ptr;\n va_start(ptr, s); / wrong anchor: must be last named parameter (num2) */\n c = va_arg(ptr, double);\n printf("%f", c);\n}
C varargs under Turbo C note: does this program compile and what does it print when characters are passed through an ellipsis?\n\n#include
\n#include
\nvoid display(int num, ...);\n\nint main()\n{\n display(4, 'A', 'a', 'b', 'c');\n return 0;\n}\nvoid display(int num, ...)\n{\n char c; int j;\n va_list ptr;\n va_start(ptr, num);\n for (j = 1; j <= num; j++)\n {\n c = va_arg(ptr, char); /* relies on Turbo C behavior */\n printf("%c", c);\n }\n}
C varargs API usage: identify the error when va_list is used without calling va_start in a variable-argument function.\n\n#include
\n#include
\nvoid varfun(int n, ...);\n\nint main()\n{\n varfun(3, 7, -11, 0);\n return 0;\n}\nvoid varfun(int n, ...)\n{\n va_list ptr;\n int num;\n num = va_arg(ptr, int); /* va_start not called */\n printf("%d", num);\n}
C varargs across functions: identify the incorrect usage when forwarding varargs and misusing va_start.\n\n#include
\n#include
\nvoid display(char *s, ...);\nvoid show(char *t, ...);\n\nint main()\n{\n display("Hello", 4, 12, 13, 14, 44);\n return 0;\n}\nvoid display(char s, ...)\n{\n show(s, ...); / attempt to forward ellipsis directly */\n}\nvoid show(char t, ...)\n{\n int a;\n va_list ptr;\n va_start(ptr, s); / wrong anchor, should be 't' */\n a = va_arg(ptr, int);\n printf("%f", a);\n}
C varargs typing: identify the primary error when the va_list variable is declared with the wrong type and used with va_start.\n\n#include
\n#include
\nvoid varfun(int n, ...);\n\nint main()\n{\n varfun(3, 7, -11.2, 0.66);\n return 0;\n}\nvoid varfun(int n, ...)\n{\n float ptr; / WRONG: should be va_list /\n int num;\n va_start(ptr, n); / invalid: ptr is not a va_list */\n num = va_arg(ptr, int);\n printf("%d", num);\n}
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