Posts

Showing posts from July, 2011

Passing variable number of arguments to a C/C++ function like printf().

Image
Introduction Many of us might have wondered at sometime or the other about how to pass a  variable  number of arguments to C++ functions : something like a  printf  function. I don't know how many are aware of this, so I decided to post an article on this topic. Background When a function is declared, the data-type and number of the passed arguments are usually fixed at compile time. But sometimes we require a function that is able to accept a variable number of arguments. The data-type and/or number of the passed arguments are provided at the run-time. Through this article, I will try to show you how to create a C function that can accept a variable number of arguments. The secret to passing variable number and type of arguments is the   stdarg   library. It provides the va_list data-type, which can contain the list of arguments passed into a function. The   stdarg  library also provides several macros :   var_arg ,  va_start , and   va_end   that are useful for mani