C/stdarg.h
C | ||
---|---|---|
Имя | Описание | Совместимость |
---|---|---|
va_list |
тип для итерационных аргументов | C89 |
stdarg.h
макросы
Имя | Описание | Совместимость |
---|---|---|
va_start |
Start iterating arguments with a va_list |
C89 |
va_arg |
Возвращает аргумент | C89 |
va_end |
Освобождает va_list |
C89 |
va_copy |
Копирует контент из одного va_list в другой |
C99 |
Accessing the arguments
To access the unnamed arguments, one must declare a variable of type va_list in the variadic function. The macro va_start is then called with two arguments: the first is the one declared of the type va_list, the second is the name of the last named parameter of the function. After this, each invocation of the va_arg macro yields the next argument. The first argument to va_arg is the va_list and the second is the type of the next argument passed to the function. Finally, the va_end macro must be called on the va_list before the function returns. (It is not required to read in all the arguments.)
C99 provides an additional macro, va_copy, which can duplicate the state of a va_list. The macro invocation va_copy(va2, va1) copies va1 into va2.
There is no mechanism defined for determining the number or types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary. Common conventions include:
- Use of a printf or scanf-like format string with embedded specifiers that indicate argument types.
- A sentinel value at the end of the variadic arguments.
- A count argument indicating the number of variadic arguments.
Type safety
Some C implementations provide C extensions that allow the compiler to check for the proper use of format strings and sentinels. Barring these extensions, the compiler usually cannot check whether the unnamed arguments passed are of the type the function expects, or convert them to the required type. Therefore, care should be taken to ensure correctness in this regard, since undefined behavior results if the types do not match. For example, if passing a null pointer, one should not write simply NULL (which may be defined as 0) but cast to the appropriate pointer type. Another consideration is the default argument promotions applied to the unnamed arguments. A float will automatically be promoted to a double. Likewise, arguments of types narrower than an int will be promoted to int or unsigned int. The function receiving the unnamed arguments must expect the promoted type.
GCC has an extension that checks the passed arguments:
- format(archetype, string-index, first-to-check)
- The format attribute specifies that a function takes printf, scanf, strftime or strfmon style arguments which should be type-checked against a format string. For example, the declaration:
extern int
my_printf (void *my_object, const char *my_format, ...)
__attribute__ ((format (printf, 2, 3)));