Home

vaarg

Vaarg, usually written as va_arg, refers to a macro in the C standard library used to retrieve the next argument from a variadic function’s parameter list. It is defined in stdarg.h and works with a va_list object, which represents the current position inside the argument list. va_start initializes this position and va_end cleans up when finished. A related utility, va_copy, can duplicate the va_list for safe, independent traversal.

Usage pattern typically looks like this: declare a va_list variable, call va_start with the last named parameter,

Important considerations include type safety and promotion rules. The type supplied to va_arg must match the

In C++, variadic arguments are usually handled by variadic templates rather than va_arg, which is C-specific.

repeatedly
fetch
arguments
with
va_arg,
and
finally
call
va_end.
For
example,
in
a
function
declared
as
void
f(int
count,
...),
you
would
initialize
a
va_list,
then
loop
count
times,
retrieving
each
argument
with
va_arg(ap,
int)
(assuming
the
arguments
are
ints).
The
type
argument
to
va_arg
must
match
the
actual
type
of
the
corresponding
parameter.
promoted
type
of
the
corresponding
argument.
Due
to
default
argument
promotions,
char
and
short
become
int,
and
float
becomes
double;
retrieving
with
a
different
type
leads
to
undefined
behavior.
va_list’s
representation
is
implementation-defined,
so
code
should
not
rely
on
its
internal
structure.
If
the
variadic
list
is
not
accompanied
by
a
count
or
sentinel
value,
the
function
cannot
determine
the
end
of
arguments
without
external
information.
va_arg
remains
a
core
tool
for
implementing
printf-like
functions
and
other
variadic
interfaces
in
C,
with
portability
governed
by
standard
header
usage
and
careful
type
management.