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

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

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

  1. c语言删除数组重复元素并升序,C语言编程题: 编写函数,从一个已经排序的数组中删去某数后,该数组仍然有序....

    满意答案 nanhrui57q 2014.01.10 采纳率:57%    等级:12 已帮助:8634人 #include #include int compear(void *, void *); ...

  2. python文件定位函数_C语言中文件定位函数总结

    C语言中文件定位函数主要是:fseek, ftell, fsetpos, fgetpos. 先来讲前两个函数,这是最基本的定位函数: fseek函数:能把文件指针移动到文件任何位置,其原型是:int ...

  3. c语言程序中注释定义,C语言语法基础:函数注释等等

    任何语言的入门大部分都是从Hello World开始的,我们学习到C语言也不例外.现在很多人说C语言很难,建议Java或者是Python入门.四喜想说的是,万事开头难,如果以C语言入门,之后的其他语言 ...

  4. c语言里主函数指什么,C语言里的主函数是什么

    main(),在c语言中称之为"主函数",一个c程序有且仅有一个main函数,任何一个c程序总是从main函数开始执行,main函数后面的一对圆括号不能省略. 具体形式如下: m ...

  5. Linux c语言 beep,用Beep()函数来实现c语言编写歌曲

    用Beep()函数来实现c语言编写歌曲Beep()函数是一个可以控制扬声器发出提示音的函数,包含在windows.h内.实际上,所有用到Sleep()函数的地方,都可以用这个,待后面细细说来. 要准备 ...

  6. codeblocks printf函数打印不出来_最全C语言基本程序交互函数之输出到屏幕

    前言 上一栏目主要讲解了各种数据类型的知识,大家先闭眼回顾一下数据类型的知识哦.本章节主要内容是讲解程序的基本交互设计之程序的输出.程序和人交互无非就是通过外设进行输入信息,C语言中基本的交互的基本流 ...

  7. c语言用getchar函数输入两个字符c1,c语言:用getchar函数读入两个字符给c1,c2,用putchar和printf输出。思考问题...

    用getchar函数读入两个字符给c1,c2,分别用putchar和printf输出这两个字符.思考以下问题: (1)变量c1和c2定义为字符型还是整型?或二者皆可? (2)要求输出c1和c2的ASC ...

  8. C语言再学习 -- printf、scanf占位符

    参看:printf().scanf()用法总结及其格式转换说明符 一.printf() printf()的返回值为输出的字符个数: 例:rv = printf ("hello"); ...

  9. 如何用单片机写出类似printf的函数

    实现功能:stm32用类似printf的函数,通过串口发送数据. 首先,让我们在qt中用C语言模拟串口输出(用printf("%c",ch)代替void USART_SendDat ...

  10. c语言double类型printf问题

    背景:今天在用c语言写程序时遇到了一个问题:printf("%lf",double(r)):竟然输出乱码(0.000000),浏览了几十个网页之后,大致找到了问题原因. 结论:在c ...

最新文章

  1. Java面试题之多线程同步和互斥有几种实现方法,都是什么?
  2. Spring Boot神操作-多个数据源Service层封装
  3. (三)ajax请求不同源之websocket跨域
  4. wxWidgets:wxWebView类用法
  5. Leetcode226. 翻转二叉树(递归、迭代、层序三种解法)
  6. 为什么需要python?它在人工智能与机器学习的优势是什么?
  7. php 文件hash,PHP HASH算法实现代码分享
  8. 47. 避免产生直写型(write-only)的代码
  9. cocos2dx-3.0(8)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
  10. 视频监控安防平台-国标35114(GB35114)A级检测
  11. 使用keil4 下载.hex文件
  12. 61php飞信发送类(phpfetion)v1.5,资源索引 L_PC6下载
  13. 24AA512/24LC512/24FC512系列器件中文翻译
  14. 算法入门篇:排序算法(一)
  15. debain下装memcached
  16. tomcat安全加固手册
  17. H5案例分享—你的数学是语文老师教的吗?
  18. java dcm转bmp源码_DCM文件转图像,并高清处理图像
  19. Matlab论文插图绘制模板第50期—分簇散点图(Swarmchart)
  20. 靠本事整塌,靠苦逼重搭

热门文章

  1. 一位清华贫困生的独白,风雨清华路!
  2. 西湖大学能招本科生了!首次确定 5 大本科专业
  3. 新手必看,17 个常见的 Python 运行时错误
  4. 无监督学习距离监督学习还有多远?Hinton组新作解读
  5. 应有尽有!这可能是最全的 AI 面试笔记了
  6. Django项目test中的mock概述
  7. python记录当前系统时间 生成照片直接命名
  8. 爬虫-selenium初步学习与使用!
  9. OSPF协议将其管理的网络划分为不同类型的若干区域(Area),其中标准区域特点是(64);存根区域(stub)的特点是(65)。【答案】C B
  10. GNN|如何做的比卷积神经网络更好?