Stdarg.h
From Wikipedia, the free encyclopedia
C++ Standard Library headers |
C Standard Library headers |
---|
assert.h |
ctype.h |
errno.h |
float.h |
limits.h |
locale.h |
math.h |
setjmp.h |
signal.h |
stdarg.h |
stddef.h |
stdio.h |
string.h |
stdlib.h |
time.h |
- The correct title of this article is stdarg.h. The initial letter is shown capitalized due to technical restrictions.
NOTE: This also refers to varargs.h
stdarg.h is a library from the C standard library of the C programming language that serves for functions to have a variable and possibly infinite number of arguments.
Contents |
[edit] The '...' operator
All functions in stdarg.h or varargs.h should be in a function which has at least one "real" argument and the last argument being "..."
For example:
int check (int a, double b, ...) ;
void see (int a, ...) ;
char *wrong (...) ; //this is wrong, is must have at least one real argument
To call check, you could put
check (a, b, c, d, e) ;
check (a, b, c) ;
check (a, b) ;
[edit] stdarg.h types
Name | Description | Compatibility |
---|---|---|
va list |
type for seeing arguments | K&R C |
[edit] stdarg.h macros
Name | Description | compatibility |
---|---|---|
va start |
Start seeing the va_list | K&R C |
va end |
Stop seeing the va_list | K&R C |
va copy |
copy contents from a va_list to another | C99 |
[edit] Example
#include <stdio.h> #ifdef LINUX //check the operative system for seeing what to include #include <varargs.h> //linux args list macros #else #include <stdarg.h> //windows args list macros #endif void printargs (int arg1, ...) //print all int type args, finishing with -1 { va_list s ; //s is an argument list va_start (s, arg1) ; //1º arg: the va_list. 2º arg: the last "real" argument on the function int g = arg1 ; //the int to print, turns into the first arg while (g != -1) //while -1 doesn't appear { printf ("%d ", g) ; //print g g = va_arg (s, int) ; //1º arg: the va_list. 2º arg: the type to be seen } printf ("\n") ; //and print a new line } int main() { printargs (5, 2, 14, 84, 97, 15, 24, 48, -1) ; printargs (84, 51, -1) ; printargs (-1) ; printargs (1, -1) ; }
The following program will make the output:
5 2 14 84 97 15 24 48 84 51
1