How to Use Variable Parameter Functions in C++


Most functions accept a fixed number of arguments. Some applications, however, require functions to accept variable parameters. A well-known example is the print function of C. For such cases, C++ offers a powerful solution that lets the compiler decipher the arguments at run time. Read on for a tutorial that demonstrates how to implement such functions.

Things You'll Need

  • Intermediate C++
  • C++ compiler with IDE, such as Microsoft Visual C++
  • Add the STDARG.H library file into your program. That's where you define the macros. On the first line of the file, insert #include < stdarg.h >.

  • Use the macros va_arg, va_start and v_end that collaborate with the va_type in stdarg.h.

  • Establish a variable of type va_list to function as a list marker.

  • Access the individual parameters by using the va_arg macro. Find a way to tell the va_arg macro what type of argument you want it to retrieve to supply the right number of bytes from the stack.

  • Cast the result from Step 4 to the type you want it to be.

  • Call the va_end macro to end the processing of variable parameters.

  • Look at the example below that demonstrates Steps 2 to 6. Function myprintf accepts a format string that specifies the type and position of each argument. Following the format string is the list of arguments. The function prints the arguments. For simplicity it can only process int, float and char:

    // declaration

    void myprintf( char format, ... );

    // defintion

    void myprintf( char
    format, ... ){

    va_list vl;

    int i;

    va_start( vl, args );

    for( i = 0; args[i] != '\0'; ++i ){

    union any_t {

    int i;

    float f;

    char c;

    } any;

    if( args[i]=='i' ){

    any.i = va_arg( vl, int );<br>
    printf( "%i\n", any.i );<br>


    if( args[i]=='f' ){

    any.f = va_arg( vl, float );<br>
    printf( "%f\n", any.f );<br>


    if( args[i]=='c' ){

    any.c = va_arg( vl, char );<br>
    printf( "%c\n", any.c );<br>



    throw SomeException;


    va_end( vl );


Promoted By Zergnet



You May Also Like

  • How to Declare a Static Variable in C

    Static variables are a useful part of C. They give you flexibility in making functions which return more complex data, but they...

  • How to Initialize Variables in C

    C does not initialize variables automatically, so if you do not initialize them properly, you can get unexpected results. Fortunately, C makes...

  • How to Use Functions in C++

    A function is code that can be executed repeatedly in a program. A C++ function consists of a label, an input argument...

  • How to Declare a Function in C

    Functions in C are the key to manageable structured programming. Every good program is written by taking the task and dividing it...

  • How to Use Command Line Parameters in C++

    Command-line parameters are special words that convey technical information to a program during launch. A user can pass them externally from the...

  • How to Use Void Pointers in C

    Pointers are one of the most powerful -- and most difficult -- features of the C programming language. When working with pointers,...

  • What Is the F Parameter for Chkdsk?

    Chkdsk is a DOS command that checks the computer's hard disks for errors including bad sectors and cross-linked files. The "F" parameter...

  • How to Use Callback Function in C

    Callback functions are advanced features in C programming language. A callback function helps in isolating the implementation of a library function from...

Read Article

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!