Wednesday, December 2, 2015

stdarg.h in C Programming Language

Hi guys! In this post, I will explain about the another header in C standard library. I usually use stdio.h, but now I will explain you about stdarg.h. Check this out.
stdarg.h is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. The stdarg.h header shall contain a set of macros which allows portable functions that accept variable argument lists to be written.
va_list is the variable type defined in the header stdarg.h. This is a type suitable for holding information needed by the three macros.
To access these additional arguments the macros va_start, va_arg, and va_end, declared in this header.
  1. void va_start(va_list ap, last_arg);This macro initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function. Note that va_start must be called before using va_arg and va_end.
    Parameters:
    ap - This is the object of va_list and it will hold the information needed to retrieve the additional arguments with va_arg. If ap has already been passed as first argument to a previous call to va_start and va_copy, it shall be passed to va_end before calling this function.
    last_arg - This is the last known fixed argument being passed to the function. The arguments extracted by subsquent call to va_arg are those after last_arg.
  2. void va_copy(va_list dest, va_list src);This macro initializes dest as a copy of src. The next argument to be extracted from dest is the same as the one that would be extracted from src. A function that invokes va_copy, should also invoke va_end on dest before it returns.
    Parameters:
    dest - This is an instance of the va_list type to initialize. After the call, it carries the information needed to retrieve the same additional arguments as src. If dest has already been passed as first argument to a previous call to va_start or va_copy , it should be passed to va_end before calling this function.
    src - This is the source va_list that will be used to initialize dest. Object of type va_list that already carries information to retrieve additional arguments with va_arg.
  3. type va_arg(va_list ap, type);This macro retrieves the next argument in the parameter list of the function with the type type. Note that ap must be initialized with va_start. If there is no next argument, then the result is undefined.
    Parameters:
    ap - This is the object of type va_list with information about the additional arguments and their retrieval state. This object should be initialized by an initial call to va_start before the first call to va_arg and not have been released with va_end.
    type - This is a type name. This type name is used as the type of the expression, this macro expands to. For a type expression to be suitable for its use with va_arg, it must be such that type produces a pointer to type. The type should be compatible with type of the extracted argumen, or one be the unsigned version of the other, or one be a void pointer and the other some other point type.
  4.  void va_end(va_list ap);This macro allows a function with variable arguments which used the va_start macro to return. If va_end is not called before returning from the function, the result is undefined. The variable argument list ap may no longer be used after a call to va_end without a call to va_start.
    Parameters:
    ap - This is the va_list object previously initialized by va_start in the same function. 
There are some example about stdarg.h
The first example

and the result is...


Second example

and the result is...

 Okay, that's all about stdarg.h. Hope u will understand about stdarg.h guys! Have a great time with coding! See ya at the next post! 

No comments:

Post a Comment