stdarg.h

The stdarg.h file defines the type va_list and the macros va_arg(), va_end(), and va_start(). The type va_list implements a pointer to one argument of an open parameter list. The macro va_start() initializes a variable of type va_list to point to the first open parameter, given the last explicit parameter and its type as arguments. The macro va_arg() returns one open parameter, given its type and also makes the va_list argument pointing to the next parameter. The va_end() macro finally releases the actual pointer. For all implementations, the va_end() macro does nothing because va_list is implemented as an elementary data type and therefore it must not be released. The va_start() and the va_arg() macros have a type parameter, accessed only with sizeof().

Listing: Example using stdarg.h
char sum(long p, ...) {
  char res=0;

  va_list list= va_start()(p, long);

  res= va_arg(list, int); // (*)

  va_end(list);

  return res; 

}

void main(void) {

  char c = 2;

  if (f(10L, c) != 2) Error();

}

In the line (*), va_arg must be called with int, not with char. Because of the default argument-promotion rules of C, integral types pass an int at least and floating types pass a double at least. In other words, using va_arg(..., char) or va_arg(..., short) yields undefined results in C. Be cautious when using variables instead of types for va_arg(). In the example above, res= va_arg(list, res) is incorrect unless res has type int and not char.