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?

Difficulty: Easy

Correct Answer: Include the ellipsis (...); you must not drop it

Explanation:


Introduction / Context:
The compiler needs explicit syntax to know a function is variadic. In C, that syntax is the ellipsis ... at the end of the parameter list. Omitting it declares a non-variadic function, and calling it with extra arguments produces undefined behavior.



Given Data / Assumptions:

  • Standard header stdarg.h is used inside the function body.
  • The function will call va_start using the last named parameter.


Concept / Approach:
The ellipsis must appear in both the prototype (if one exists) and the definition to communicate the calling convention to both the compiler and linker. Without it, callers will pass no extra arguments and the callee cannot legally use va_start because there is no last named parameter before ... .



Step-by-Step Solution:
Declare: int sum(int count, ...);Define: int sum(int count, ...) { va_list ap; va_start(ap, count); /* use va_arg */ va_end(ap); }Do not omit the ellipsis in either place.



Verification / Alternative check:
Review standard library declarations (printf, execl, etc.); all include ... in their prototypes to signal variadic status.



Why Other Options Are Wrong:
The compiler cannot infer variadic behavior automatically. Attributes are nonportable. Floating point is not a condition for using ... . Using ... only in the definition mismatches the prototype and leads to undefined behavior.



Common Pitfalls:
Leaving the prototype non-variadic while defining a variadic function; forgetting that default promotions change the types to request via va_arg.



Final Answer:
Include the ellipsis (...); you must not drop it

Discussion & Comments

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