'va_list'에 해당되는 글 1건

  1. 2007.06.18 [VA.C] example for variable number of arguments
C, C++2007. 6. 18. 16:31


// Example

// http://msdn.micrsoft.com/

/* VA.C: The program below illustrates passing a variable
 * number of arguments using the following macros:
 *      va_start            va_arg              va_end
 *      va_list             va_dcl (UNIX only)
 */

#include <stdio.h>
#define ANSI            /* Comment out for UNIX version     */
#ifdef ANSI             /* ANSI compatible version          */
#include <stdarg.h>
int average( int first, ... );
#else                  
/* UNIX compatible version          */
#include <varargs.h>
int average( va_list );
#endif

void main( void )
{
  
/* Call with 3 integers (-1 is used as terminator). */
   printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

  
/* Call with 4 integers. */
   printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

  
/* Call with just -1 terminator. */
   printf( "Average is: %d\n", average( -1 ) );
}

/* Returns the average of a variable list of integers. */
#ifdef ANSI             /* ANSI compatible version    */
int average( int first, ... )
{
   int count
= 0, sum = 0, i = first;
   va_list marker;

   va_start( marker,
first );     /* Initialize variable arguments. */
   while( i != -1 )
   {
      sum
+= i;
      count
++;
      i
= va_arg( marker,  int ); /* get an integer argument */
   }
   va_end( marker );             
/* Reset variable arguments.      */
   return( sum ? (sum / count) : 0 );
}
#else      
/* UNIX compatible version must use old-style definition.  */
int average( va_alist )
va_dcl
{
   int i, count, sum;
   va_list marker;

   va_start( marker );           
/* Initialize variable arguments. */
   for( sum = count = 0; (i = va_arg( marker, int)) != -1; count++ )
      sum
+= i;
   va_end( marker );             
/* Reset variable arguments.      */
   return( sum ? (sum / count) : 0 );
}
#endif


// Output
//
// Average is: 3
// Average is: 8
// Average is: 0

'C, C++' 카테고리의 다른 글

Vista 에서 Orcas UAC Elevation 테스트  (0) 2007.07.12
Orcas 베타 버전 압축에 RAR 가??!!  (0) 2007.07.02
[VA.C] example for variable number of arguments  (0) 2007.06.18
overloading on the c++  (0) 2007.06.14
Dialog Units  (0) 2007.06.13
현재 윈도우즈 버전 얻기 예제  (0) 2007.05.19
Posted by 어쩌다보니 Jay Two

댓글을 달아 주세요