C语言可变参数获取及其简单处理:
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#define MYPRINTF(a, format, ...) \
do \
{ \
char* str = (char*)malloc(128); \
sprintf(str, format, __VA_ARGS__); \
printf("MYPRINTF --> %s", str); \
} \
while(0)
void myPrintf(int i, char *format, ...)
{
va_list arg_list;
va_start(arg_list, format);
vprintf(format, arg_list);
va_end(arg_list);
}
void myPrintf1(int i, char *format, ...)
{
char msg[256];
va_list arg_list;
va_start(arg_list, format);
vsnprintf(msg, sizeof(msg), format, arg_list);
va_end(arg_list);
printf("myPrintf1 --> %s", msg);
}
int _tmain(int argc, _TCHAR* argv[])
{
MYPRINTF(1, "hello, %d, %d, %s\n", 1, 1, "test");
myPrintf(1, "hello, %d, %d, %s\n", 1, 2, "test");
myPrintf1(1, "hello, %d, %d, %s\n", 1, 3, "test");
return 0;
}