liveBook · Manning

Stdarg.h

As explained in Modern C, Third Edition

stdarg.h is the C standard header that declares the types and macros for processing variable argument lists.

A variadic function accepts a fixed parameter list followed by ..., then uses stdarg.h to access the hidden arguments through va_list. For formatted output, pass that list to vfprintf instead of parsing each argument manually. section 17.4.2

void va_start(va_list ap, ...);
void va_end(va_list ap);
\textit{type} va_arg(va_list ap, \textit{type});
void va_copy(va_list dest, va_list src);

The core interface consists of va_list plus va_start, va_end, va_arg, and va_copy; initialize, retrieve, copy, and finally release argument-list state. section 17.4.2

Use the interface with explicit portability and correctness constraints:

  • Initialize a va_list with va_start, retrieve arguments with va_arg, and finish with va_end. section 17.4.2
  • Since C23, va_start may omit the traditional second argument naming the last parameter before .... section 17.4.2
  • Do not rely on implicit conversions in variadic calls: each argument must be forced to the expected type for portable behavior. section 17.4.2
  • Prefer existing formatted-printing functions and avoid designing new variadic interfaces when a non-variadic alternative is available. section 17.4.2

Forwarding a variable argument list

A wrapper around formatted output can create a va_list with va_start, pass it to vfprintf, and call va_end afterward. This avoids implementing argument-by-argument format handling. section 17.4.2

How Modern C, Third Edition covers it

How deep it goes
Defines va_list and four access macros, illustrates forwarding, and flags type and lifecycle constraints without detailing representation internals.
Examples
Uses printf declarations, nonportable pointer and integer calls, a vfprintf wrapper, and summing n double values.
What it assumes
Assumes familiarity with C functions, parameters, ellipses, formatted output, and type conversions.
What you can do after
Apply va_list to implement or forward variadic arguments while preserving required initialization, cleanup, and type constraints.
How it connects
Connects variadic functions with variadic macros, formatted printing, type conventions, and compiler diagnostics.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.