马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第一个实例:#include <stdio.h>
#include <Windows.h>
#include<tchar.h>
int sum(int number,...)
{
va_list argList;
int i;
int sum1 = 0;
va_start(argList,number);
for(i=0; i<number;i++)
{
sum1 += va_arg(argList,int);
}
va_end(argList);
return sum1;
}
int main(void)
{
int k = 0;
k = sum(3,1,2,3);
printf("%d\n",k);
system("pause");
return 0;
}
运行结果:
实例二:#include <stdio.h>
#include <Windows.h>
#include<tchar.h>
void print (char *str1,...)
{
va_list argList ;
int sum = 0;
va_start (argList,str1);
sum = va_arg(argList,int );
printf("%d\n",sum);
sum = va_arg(argList,int );
printf("%d\n",sum);
sum = va_arg(argList,int );
printf("%d\n",sum);
return;
}
int main(void)
{
int a=1,b=2,c=3;
print("",a,b,c);
system("pause");
return 0;
}
运行结果:
实例三:#include<stdio.h>
#include <stdarg.h>
#include <windows.h>
#define bufsize 80
char buffer[bufsize];
int vspf(char *fmt, ...)
{
va_list argptr;
int cnt;
va_start(argptr, fmt);
cnt = vsnprintf(buffer,bufsize ,fmt, argptr);
va_end(argptr);
return(cnt);
}
int main(void)
{
int inumber = 30;
float fnumber = 90.0;
char string[4] = "abc";
vspf("%d %f %s", inumber, fnumber, string);
printf("%s\n", buffer);
system("pause");
return 0;
}
运行结果:
|