经验:变长度参数

#include <stdio.h>

int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int dprintf(int fd, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

#include <stdarg.h>

int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vdprintf(int fd, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);

 

例子:

__attribute__((format(printf, 3, 4)))
int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)

{

va_list args;

va_start(args, fmt);

vsnprintf(nwlog + strlen(nwlog), NWLEN - strlen(nwlog) - 1, fmt, args);  //正确

//snprintf(nwlog + strlen(nwlog), NWLEN - strlen(nwlog) - 1, fmt, args); //错误!

va_end(args);
return ret;

}

posted on 2023-10-18 14:23  huangguanyuan  阅读(6)  评论(0编辑  收藏  举报

导航