va_arg, va_copy, va_start, and va_end : Variable Number of Arguments « Function « C Tutorial

Home
C Tutorial
1.Language
2.Data Type
3.String
4.printf scanf
5.Operator
6.Statement
7.Array
8.Function
9.Structure
10.Pointer
11.Memory
12.Preprocessor
13.File
14.Data Structure
15.Search Sort
16.Wide Character String
17.assert.h
18.ctype.h
19.math.h
20.setjmp.h
21.signal.h
22.stdio.h
23.stdlib.h
24.string.h
25.time.h
26.wctype.h
C / ANSI-C
C++
C++ Tutorial
Visual C++ .NET
C Tutorial » Function » Variable Number of Arguments 
8.9.1.va_arg, va_copy, va_start, and va_end
ItemValue
Header filestdarg.h
Declartiontype va_arg(va_list argptr, type);
void va_copy(va_list target, va_list source);
void va_end(va_list argptr);
void va_start(va_list argptr, last_parm);


Use va_arg(), va_start(), and va_end() macros together to pass a variable number of arguments to a

function. For example, printf(). The type va_list is defined by .

#include <stdio.h>
  #include <stdarg.h>

  double sum_series(int num, ...);

  /* Variable length argument example - sum a series. */
  int main(void)
  {
    double d;

    d = sum_series(50.50.250.1250.06250.03125);

    printf("Sum of series is %f.\n", d);
    return 0;
  }

  double sum_series(int num, ...)
  {
    double sum=0.0, t;
    va_list argptr;

    /* initialize argptr */
    va_start(argptr, num);

    /* sum the series */
    for; num; num--) {
      t = va_arg(argptr, double)/* get next argument */
      sum += t;
    }

    /* do orderly shutdown */
    va_end(argptr);
    return sum;
  }
Sum of series is 0.968750.
8.9.Variable Number of Arguments
8.9.1.va_arg, va_copy, va_start, and va_end
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.