int vprintf ( const char * format, va_list arg );
描述:
将可变参数列表的格式化数据打印到stdout
将格式指向的C字符串写入标准输出(stdout),以printf的方式替换任何格式说明符,
但使用由arg标识的变量参数列表中的元素而不是附加的函数参数。
在内部,函数从arg标识的列表中检索参数,就好像va_arg被使用了一样,因此arg的状态很可能被调用所改变。
在任何情况下,arg都应该在调用之前的某个时刻由va_start初始化,并且在调用之后的某个时刻,预计会由va_end释放。
参数:
format
包含格式字符串的C字符串,其格式字符串与printf中的格式相同。
arg
标识使用va_start初始化的变量参数列表的值。
va_list是在<cstdarg>中定义的特殊类型。
返回值:
成功后,返回写入的字符总数。
如果发生写入错误,则会设置错误指示符(ferror)并返回负数。
如果在编写宽字符时发生多字节字符编码错误,则将errno设置为EILSEQ,并返回负数。
/* vprintf example */
#include <stdio.h>
#include <stdarg.h>
void WriteFormatted ( const char * format, ... )
{
va_list args;
va_start (args, format);
vprintf (format, args);
va_end (args);
}
int main ()
{
WriteFormatted ("Call with %d variable argument.\n",1);
WriteFormatted ("Call with %d variable %s.\n",2,"arguments");
return 0;
}
The example illustrates how the WriteFormatted can be called with a different number of arguments, which are on their turn passed to the vprintf function, showing the following output:
Call with 1 variable argument. Call with 2 variable arguments. |