Difficulty: Easy
Correct Answer: False
Explanation:
Introduction / Context:
This statement probes understanding of how va_arg
works. The macro does not “jump” to the last argument; it sequentially retrieves the next argument of a specified type from the variable argument list represented by a va_list
.
Given Data / Assumptions:
void logf(int n, ...);
Concept / Approach:va_arg(ap, T)
returns the next argument of type T
and advances the va_list
state. It has no knowledge of how many arguments were passed; determining the count is up to the function (via an explicit count parameter, a sentinel value, or a format string). Thus, the claim that va_arg()
is for “the last argument” is false.
Step-by-Step Solution:
Initialize with va_start(ap, last_named_param);
Iterate: for (...){ value = va_arg(ap, expected_type); }
Stop based on a known count or a sentinel condition.Finish with va_end(ap);
Verification / Alternative check:
Review any formatted print implementation strategy: it repeatedly calls va_arg
for each conversion specifier, never “seeking to the last”.
Why Other Options Are Wrong:
Architecture width (32-bit) and parity of count are irrelevant. The mechanism is the same across platforms that conform to the C standard.
Common Pitfalls:
Misconstruing va_arg
as random-access; it is strictly sequential. Passing mismatched types against the expected types is undefined behavior.
Final Answer:
False
Discussion & Comments