#include "stdafx.h" #include <stdarg.h> int Myprintf(char* format,...) { va_list ap; va_start(ap, format); int n = vprintf(format, ap); va_end(ap); return n; } int Mysprintf(char* buffer, char* format,...) { va_list ap; va_start(ap, format); int n = vsprintf(buffer,format,ap); va_end(ap); return n; } void subfunc (char *fmt, va_list argp) { while (fmt != NULL) { //char* arg = va_arg (argp,char*); /* 从argp中逐一取出所要的参数 */ switch (*fmt) { case 's': { char* pTmp = (char*) va_arg(argp, char*); printf("Printing a string: %s/n", (char*) va_arg(argp, char*)); } break; //case 'c': // printf("Printing a char: %c/n", (char) va_arg(argp, int)); // break; //case 'd': // printf("Printing a integer: %d/n", (int) va_arg(argp, int)); // break; default: break; } fmt++; } } void mainfunc (char *fmt, ...) { va_list argp; va_start (argp, fmt); /* 将可变长参数转换为va_list */ subfunc (fmt, argp); /* 将va_list传递给子函数 */ va_end (argp); } int _tmain(int argc, _TCHAR* argv[]) { int n = 2010; //Myprintf("hello world!--%d",2010); char* pName = "xmj"; char szCopy[20]; Mysprintf(szCopy,"hello world!--%s",pName); //mainfunc("hello world!--%s",pName); return 0; }