先上一段代码:
#include<cstdarg>
#include<iostream>
#include<string>
using namespace std;
void error(char* format,...){//至少要有一个参数,后边的...表示参数可变
va_list ap;
int d,flag;
char c, *s;
va_start(ap,*format);//从args参数开始后面都是可变参数,va_start,va_end都为宏
while (*format){
switch (*format) {
case ‘s‘: /* string */
if(!flag) break;
s = va_arg(ap, char *);
cout<<s;flag=0;
break;
case ‘d‘: /* int */
if(!flag) break;
d = va_arg(ap, int);
cout<<d;flag=0;
break;
case ‘c‘: /* char */
/* need a cast here since va_arg only
takes fully promoted types */
if(!flag) break;
c = (char) va_arg(ap, int);
cout<<c;flag=0;
break;
case ‘%‘:
flag=1;
break;
default:
putchar(*format);
}
format++;}
putchar(‘\n‘);
/*for(i=0;i<args;i++){//遍历所有其他参数
//printf("%d",va_arg(ap,int));
}*/
va_end(ap);//destroy va_list;
}
int main(){
char s[]="rich";
error("I am %s,I have %d %c",s,100,‘$‘);
//getchar();
return 0;
}
其中error()函数像printf一样打印出格式化字符串。
#include <stdarg.h>
头文件中定义了一下一些宏,注意不是函数
void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);
有趣的是va_arg宏,每次处理后ap指向下一个参数,请看man手册:
va_arg()
The va_arg() macro expands to an expression that has the type and value
of the next argument in the call. The argument ap is the va_list ap
initialized by va_start(). Each call to va_arg() modifies ap so that
the next call returns the next argument. The argument type is a type
name specified so that the type of a pointer to an object that has the
specified type can be obtained simply by adding a * to type.
The first use of the va_arg() macro after that of the va_start() macro
returns the argument after last. Successive invocations return the
values of the remaining arguments.
If there is no next argument, or if type is not compatible with the
type of the actual next argument (as promoted according to the default
argument promotions), random errors will occur.
If ap is passed to a function that uses va_arg(ap,type) then the value
of ap is undefined after the return of that function.