解释一下c语言 for(;;) printf("*");,printf()函数的一个问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

va_arg retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve arguments from the list.

After all arguments have been retrieved, va_end resets the pointer to NULL.

The UNIX System V macros, defined in VARARGS.H, operate somewhat differently:

Any required arguments to the function can be declared as parameters in the usual way.

The last (or only) parameter to the function represents the list of optional arguments. This parameter must be named va_alist (not to be confused with va_list, which is defined as the type of va_alist).

va_dcl appears after the function definition and before the opening left brace of the function. This macro is defined as a complete declaration of the va_alist parameter, including the terminating semicolon; therefore, no semicolon should follow va_dcl.

Within the function, va_start sets arg_ptr to the beginning of the list of optional arguments passed to the function. va_start must be used before va_arg is used for the first time. The argument arg_ptr must have va_list type.

va_arg retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve the arguments from the list.

After all arguments have been retrieved, va_end resets the pointer to NULL.

Example

/* 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 

#define ANSI /* Comment out for UNIX version */

#ifdef ANSI /* ANSI compatible version */

#include 

int average( int first, ... );

#else /* UNIX compatible version */

#include 

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);

}

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

Argument Access Routines

See Also vfprintf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值