Varargs.h
   HOME

TheInfoList



OR:

stdarg.h is a header in the
C standard library The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
of the
C programming language C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the header cstdarg. The contents of stdarg.h are typically used in
variadic function In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. The term ''var ...
s, though they may be used in other functions (for example,
vprintf The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header . The functionality descends from a "portable I/O package" written by Mike Lesk at ...
) called by variadic functions.


Creating variadic functions

Variadic functions In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. The term ''var ...
are functions which may take a variable number of arguments and are declared with an
ellipsis The ellipsis (, plural ellipses; from , , ), rendered , alternatively described as suspension points/dots, points/periods of ellipsis, or ellipsis points, or colloquially, dot-dot-dot,. According to Toner it is difficult to establish when t ...
in place of the last parameter. An example of such a function is
printf printf is a C standard library function that formats text and writes it to standard output. The function accepts a format c-string argument and a variable number of value arguments that the function serializes per the format string. Mism ...
. Respectively, declarations and definitions are done similarly as such int check(int a, double b, ...); int check(int a, double b, ...) According to the standard, varadic functions without any named parameters are not allowed in C17 and earlier, but in C++ and C23 such a declaration is permitted. In C, a comma must precede the ellipsis if a named parameter is specified, while in C++ it is optional. Some
K&R C C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
style function declarations do not use ellipses.


stdarg.h types


stdarg.h macros


Accessing the arguments

According to the standard, to access the unnamed arguments it can be through a variable of type va_list in the variadic function, with macro va_start also provided as the last named parameter of the function. In C23 the second argument is optional and will not be evaluated. 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. As the last step, the va_end macro must be called on the va_list before the function returns. Note that it is not required to read in all the arguments.
C99 C99 (previously C9X, formally ISO/IEC 9899:1999) is a past version of the C programming language open standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps implementations mak ...
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 defined method for counting or classifying the unnamed arguments passed to the variadic function. The function should simply determine this somehow, the means of which vary. Common conventions include: * Use of a
printf printf is a C standard library function that formats text and writes it to standard output. The function accepts a format c-string argument and a variable number of value arguments that the function serializes per the format string. Mism ...
or
scanf scanf, short for scan formatted, is a C standard library function that reads and parses text from standard input. The function accepts a format string parameter that specifies the layout of input text. The function parses input text and l ...
-like format string with embedded specifiers that indicate argument types. * A
sentinel value In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically ...
at the end of the variadic arguments. * A count argument indicating the number of variadic arguments.


Passing unnamed arguments to other calls

As the size of the unnamed argument list is generally unknown, the calling conventions employed by most compilers do not permit determining the size of the unnamed argument block pointed at by va_list inside the receiving function. As a result there is also no reliable, generic way to forward the unnamed arguments into another variadic function. Even where determining the size of the argument list is possible by indirect means (for example, by parsing the format string of fprintf()), there is no portable way to pass the dynamically determined number of arguments into the inner variadic call, as the number and size of arguments passed into such calls must generally be known at compile time. To some extent, this restriction can be relaxed by employing
variadic macro A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments. Variable-argument macros were introduced in 1999 in the ''ISO/IEC 9899 ...
s instead of variadic functions. Additionally, most standard library procedures provide v-prefixed alternative versions which accept a ''reference'' to the unnamed argument list (i.e. an initialized va_list variable) instead of the unnamed argument list itself. For example, vfprintf() is an alternate version of fprintf() expecting a va_list instead of the actual unnamed argument list. A user-defined variadic function can therefore initialize a va_list variable using va_start and pass it to an appropriate standard library function, in effect passing the unnamed argument list by reference instead of doing it by value. Because there is no reliable way to pass unnamed argument lists by value in C, providing variadic
API An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
functions without also providing equivalent functions accepting va_list instead is considered a bad programming practice.


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 In computer programming, a program exhibits undefined behavior (UB) when it contains, or is executing code for which its programming language specification does not mandate any specific requirements. This is different from unspecified behavior, ...
results if the types do not match. For example, if the expected type is int *, then a null pointer should be passed as (int *)NULL. Writing just NULL would result in an argument of type either int or void *, neither of which is correct. 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))); causes the compiler to check the arguments in calls to my_printf for consistency with the printf style format string argument my_format


Example

#include #include /*Get sum of variables*/ int sum(int count, ...) int main(void) This program should get the following output:
4


varargs.h

Outdated versions of
POSIX The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
defined the legacy header varargs.h, which dates from before the standardization of C and provides functionality similar to stdarg.h. This header is part of neither ISO C nor POSIX. The file, as defined in the second version of the
Single UNIX Specification The Single UNIX Specification (SUS) is a standard for computer operating systems, compliance with which is required to qualify for using the "UNIX" trademark. The standard specifies programming interfaces for the C language, a command-line shell, ...
, simply contains all of the functionality of C89 stdarg.h, with the exceptions that: * it cannot be used in standard C new-style definitions * the given argument may be omitted (standard C requires at least one argument) The interface is also different. For example, one would instead write: #include #include /* There is no "void" type; use an implicit int return. */ printargs(arg1, va_alist) va_dcl /* no semicolon here! */ and is called the same way. varargs.h requires old-style function definitions because of the way the implementation works. Conversely, it is not possible to mix old-style function definitions with stdarg.h.


References

{{CProLang, state=expanded Articles with example C code C standard library headers