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?

Difficulty: Easy

Correct Answer: Yes

Explanation:


Introduction / Context:
va_start requires the name of the last fixed parameter to locate the start of the unnamed argument area. Without at least one named parameter, standard C cannot correctly initialize traversal of the variadic arguments.



Given Data / Assumptions:

  • We are following the C standard’s stdarg.h facilities.
  • Some nonstandard extensions may exist, but the question targets portable, standard C.


Concept / Approach:
The macro va_start(ap, parmN) expands in a way that depends on the address and calling convention of parmN (the last named parameter). Therefore, a zero-named-parameter signature like func(...) is not valid in standard C for using va_start. Portable variadic functions always declare at least one fixed parameter (e.g., int count, ... or const char *fmt, ...).



Step-by-Step Solution:
Design the API with at least one named parameter (e.g., fmt).Call va_start(ap, fmt); // requires fmt to exist.Use va_arg to retrieve each value in order.Call va_end when done.



Verification / Alternative check:
Look at the standard library’s printf family: printf(const char *fmt, ...). The existence of fmt enables correct va_start behavior. Families like execl also include at least one fixed parameter before ... .



Why Other Options Are Wrong:
“No” conflicts with the stdarg.h contract. References to compiler era or stdio.h are irrelevant. Floating-point is not special here; the rule applies to all types.



Common Pitfalls:
Trying to declare func(...) without a fixed arg; assuming the implementation can somehow infer the boundary automatically.



Final Answer:
Yes

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion