Difficulty: Easy
Correct Answer: Correct
Explanation:
Introduction / Context:
Processing variadic parameters in C requires an explicit iteration mechanism. This question verifies that you know the standard workflow: va_start to initialize traversal, va_arg to read each value, and va_end to finish.
Given Data / Assumptions:
Concept / Approach:
Calling va_start prepares a va_list to access the unnamed arguments. The only standard way to fetch each value is va_arg(list, type). There is no automatic extraction, and no reflection in C to discover argument types; the callee must know what types to request, often guided by a format string or a count parameter.
Step-by-Step Solution:
Initialize: va_list ap; va_start(ap, last_named_param).Loop: value = va_arg(ap, expected_promoted_type); process value.Repeat until your stopping condition (e.g., count reached or format parsed).Finish: va_end(ap).
Verification / Alternative check:
Compare printf and vprintf. The latter accepts a va_list created by another variadic function. Both ultimately use va_arg internally to traverse unnamed parameters.
Why Other Options Are Wrong:
“Incorrect” denies standard practice. Requiring stdio.h or 64-bit is irrelevant; stdarg.h is portable. “Extraction happens automatically” is false; va_arg is mandatory for reading values.
Common Pitfalls:
Requesting the wrong type from va_arg, forgetting default promotions (float → double), or skipping va_end.
Final Answer:
Correct
Discussion & Comments