liveBook · Manning

Va_start

As explained in Modern C, Third Edition

va_start is a C standard-library macro that initializes a variable argument list for access by va_arg and related operations.

va_start is a macro from <stdarg.h> that initializes a va_list with information about a variadic function’s hidden arguments. The initialized list can then be consumed by va_arg or passed to a library function such as vfprintf or vsnprintf. section 17.2 section 17.4.2

Initialize, forward, and finish a variable argument list

void trace(char const *format, ...) { va_list ap; va_start(ap, format); /* use the last named parameter */ vfprintf(stream, format, ap); va_end(ap); } Here va_start prepares ap without using the address operator. vfprintf performs the formatted-argument processing, while va_end closes the use of the list. Since C23, the second va_start argument may be omitted; traditionally it names the parameter immediately before the ellipsis. section 17.4.2

A variadic implementation commonly combines these <stdarg.h> operations:

  • va_start initializes a va_list for the arguments hidden behind ... . section 17.4.2
  • va_arg retrieves one value and requires its type explicitly; the implementation must track the list as values are consumed. section 17.4.2
  • va_end completes the use of the initialized list, and va_copy provides a separate list copy when needed. section 17.4.2
  • Prefer an existing formatted-output function when possible, rather than reproducing its argument analysis. section 17.2 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 compact interface listing places va_start, va_arg, va_end, and va_copy together, making their roles and signatures easy to compare. section 17.4.2

How Modern C, Third Edition covers it

How deep it goes
Explains initialization, typed retrieval, forwarding, cleanup, and the C23 parameter change, but not va_list internals.
Examples
Uses a formatted-output wrapper that initializes va_list and forwards it to vfprintf or vsnprintf.
What it assumes
Assumes familiarity with C functions and ellipsis parameters while defining va_list-based argument access from the interface upward.
What you can do after
Build a variadic wrapper that initializes, forwards, and finishes a variable argument list using <stdarg.h>.
How it connects
Connects va_start with variadic functions, va_list, va_arg, va_end, va_copy, vfprintf, and vsnprintf.

Also explained in

Compare how 2 books explain va_start →

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.