0%

变参函数

Variadic function in C

See more: Variadic functions in C

vprintf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdarg.h>
#include <stdio.h>

void myPrintf(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}

int main() {
long int time = 100;
myPrintf("Elapsed time is: %ld seconds.\n", time);

return 0;
}

Output:

1
2
$ ./a.out 
Elapsed time is: 100 seconds.

ap_list + vsnprintf

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
#include <stdarg.h>
#include <stdio.h>
#include <sys/time.h> // gettimeofday
#include <time.h>
#include <sys/socket.h>

using namespace std;

void foo(int id, const char *fmt, ...)
{
constexpr int MAXLEN = 1024;
char buf[MAXLEN];
int n;

n = snprintf(buf, MAXLEN, "INFO(%d): ", id);

va_list ap;
// ap will be the pointer to the last fixed argument of the variadic function.
va_start(ap, fmt);
n += vsnprintf(buf + n, MAXLEN - n, fmt, ap);
// This ends the traversal of the variadic function arguments.
va_end(ap);

struct timeval tv;
gettimeofday(&tv, NULL);
struct tm tm;
localtime_r(&tv.tv_sec, &tm);
char timebuf[64];
// size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
snprintf(timebuf, MAXLEN - n, "%d-%02d-%02d %d:%d:%d.%ld %s",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec, tm.tm_zone);

printf(buf, timebuf);
}

int main()
{
const char* name = "Tony";
// "%%s" is a placeholder of the timestamp for the vsnprintf function.
foo(123, "Hello %s at %%s\n", name);
// Will print:
// INFO(123): Hello Tony at 2023-10-04 21:25:23.682853 CST

return 0;
}

va_list + va_arg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdarg.h>
#include <stdio.h>

constexpr bool debug = true;

int printDebugLog(const char* fmt, ...) {
if (debug) {
// The first argument doesn't need to traverse via va_list.
printf("fmt=%s\n", fmt);
va_list ap;
va_start(ap, fmt);
long int t = va_arg(ap, long int);
va_end(ap);
printf(fmt, t);
}
return 0;
}

int main() {
long int time = 100;
printDebugLog("Elapsed time is: %ld seconds.\n", time);

return 0;
}

Output:

1
2
3
4
$ ./a.out 
fmt=Elapsed time is: %ld seconds.

Elapsed time is: 100 seconds.