liveBook · Manning

Va_list

As explained in Modern C

va_list is the C standard library type used to represent the state of traversal through a variable argument list.

Forwarding a variable argument list

int printf_debug(const char *format, ...) { va_list va; va_start(va, format); int ret = vfprintf(iodebug, format, va); va_end(va); return ret; } The function initializes traversal state after its last named parameter, forwards that state to vfprintf, and then releases it. section 16.5.2

va_list is the C library type representing traversal state for a variable argument list. stdarg.h supplies it together with macros for starting, reading, copying, and ending traversal. section 16.5.2

Use the associated macros as a lifecycle:

  • Call va_start with the va_list object and the last named parameter. section 16.5.2
  • Retrieve each value with va_arg, supplying its expected type explicitly. section 16.5.2
  • Call va_end after traversal; use va_copy when an independent traversal is needed. section 16.5.2
1   void va_start(va_list ap, parmN);
2   void va_end(va_list ap);
3   type va_arg(va_list ap, type);
4   void va_copy(va_list dest, va_list src);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":8}],[{\"line\":0,\"ch\":9},{\"line\":0,\"ch\":17}],[{\"line\":0,\"ch\":18},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":16},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":24}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":8}],[{\"line\":1,\"ch\":9},{\"line\":1,\"ch\":15}],[{\"line\":0,\"ch\":18},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":16},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":24}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":2,\"ch\":9},{\"line\":2,\"ch\":15}],[{\"line\":0,\"ch\":18},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":16},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":24}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":8}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":8}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":8}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":16}],[{\"line\":0,\"ch\":18},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":16},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":24}],[{\"line\":0,\"ch\":18},{\"line\":0,\"ch\":25}],[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":23}],[{\"line\":2,\"ch\":16},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":17},{\"line\":3,\"ch\":24}]]"}
!@%STYLE%@!

Shows the pseudo-interfaces for the four operations associated with va_list. section 16.5.2

How Modern C covers it

How deep it goes
Explains initialization, typed extraction, copying, and cleanup, but defers representation and calling-convention internals.
Examples
Uses a printf_debug wrapper and a sumIt function that accumulates n double values.
What you can do after
Operate stdarg.h and vfprintf to forward or traverse variable arguments with explicit extraction types.
How it connects
Ties va_list to variadic functions, va_start, va_arg, va_copy, va_end, vfprintf, and stdarg.h.

Also explained in

Compare how 2 books explain va_list →

Related concepts

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