Difficulty: Easy
Correct Answer: Error: Invalid declaration of fun(...)
Explanation:
Introduction / Context:
This question examines correct use of C variable-argument functions (varargs) with
. Specifically, it tests whether the function with ellipsis is declared and defined properly and whether va_start
is anchored to a valid, named parameter that precedes the ellipsis. Understanding these rules prevents undefined behavior and compilation errors in real-world code such as logging or formatting routines.
Given Data / Assumptions:
fun(...);
with no named fixed parameter.fun(...)
also uses only an ellipsis.va_start(ptr, n)
uses an identifier n
that is not a parameter.
Concept / Approach:
In standard C, a variadic function must have at least one fixed, named parameter before ...
. The macro va_start
requires that the second argument be that last named parameter. Using only ...
makes it impossible to position the va_list
at the first variable argument. Therefore, such a declaration/definition is invalid. Missing an explicit return type is also an error in modern C, but the most fundamental issue is the invalid variadic signature.
Step-by-Step Solution:
Identify function signature: fun(...)
→ no named parameter → invalid for varargs.Check va_start(ptr, n)
→ n
is not a parameter → misuse of va_start
.Conclusion → declaration/definition of fun(...)
is invalid.
Verification / Alternative check:
Correct pattern: void fun(int count, ...)
va_list ap; va_start(ap, count);
This compiles and allows safe extraction of arguments with va_arg
.
Why Other Options Are Wrong:
Error: fun() needs return type — true in modern C, but not the primary varargs error being tested. Error: ptr Lvalue required — ptr
is a proper lvalue; the issue is not lvalueness. No error — incorrect; there is a clear signature misuse.
Common Pitfalls:
Forgetting the required named parameter, anchoring va_start
to an identifier that is not a parameter, and assuming legacy implicit-int rules still apply.
Final Answer:
Error: Invalid declaration of fun(...)
Discussion & Comments