va_arg Macro

Gets the current argument.

Include

<stdarg.h>

Prototype

#define va_arg(va_list ap, Ty)

Arguments

ap
pointer to list of arguments
Ty
type of argument to be retrieved

Return Value

Returns the current argument

Remarks

va_start must be called before va_arg.

Example

#include <stdio.h>
#include <stdarg.h>

void tprint(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  while (*fmt)
  {
    switch (*fmt)
    {
      case '%':
            fmt++;
            if (*fmt == 'd')
            {
              int d = va_arg(ap, int);
              printf("<%d> is an integer\n",d);
            }
            else if (*fmt == 's')
            {
              char *s = va_arg(ap, char*);
              printf("<%s> is a string\n", s);
            }
            else
            {
              printf("%%%c is an unknown format\n",
                   *fmt);
            }
            fmt++;
            break;
      default:
            printf("%c is unknown\n", *fmt);
            fmt++;
            break;
    }
  }
  va_end(ap);
}
int main(void)
{
  tprint("%d%s.%c", 83, "This is text.", 'a');
}

Example Output

<83> is an integer
<This is text.> is a string
. is unknown
%c is an unknown format