-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdarg.cpp
57 lines (44 loc) · 889 Bytes
/
stdarg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
/**
* @brief 可变参数
*/
void print1(int count, ...)
{
va_list valist;
int value = 0.0;
printf("count:%d\n", count);
// count个参数初始化valist
va_start(valist, count);
// 访问所有赋给valist的参数
for (int i = 0; i < count; i++)
{
value = va_arg(valist, int);
printf("%d ", value);
}
printf("\n");
// 清理valist保留的内存
va_end(valist);
}
/**
* @brief 可变参数
*/
void print2(const char* fmt, ...)
{
va_list args;
char buf[1024] = {0};
printf("fmt[%s]\n", fmt);
// 初始化valist
va_start(args, fmt);
vsprintf(buf,fmt,args);
va_end(args);
printf("buf[%s]\n", buf);
}
int main()
{
const char *txh = "tanxiaohai";
print1(5, 1, 2, 3, 4, 5);
print2("%s", txh);
return 0;
}