什么时候用vsprintf

sprintf(buf,format,...);
虽然sprintf支持可变参数,但一般我们调用时其参数是确定的.
例如:
sprintf(buf,"%d",i);
将f中的可变参数转化为va_list
将valist作为参数来调用vsprintf


其实,printf就是用vprintf写的。
给个例子,这个例子是复制的:
#include <windows.h>

#include <tchar.h>

#include <stdio.h>

int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)

{

TCHAR szBuffer [1024] ;

va_list pArgList ;


// The va_start macro (defined in STDARG.H) is usually equivalent to:

// pArgList = (char *) &szFormat + sizeof (szFormat) ;


va_start (pArgList, szFormat) ;


// The last argument to wvsprintf points to the arguments


_vsntprintf ( szBuffer, sizeof (szBuffer) / sizeof (TCHAR),

szFormat, pArgList) ;


// The va_end macro just zeroes out pArgList for no good reason

va_end (pArgList) ;

return MessageBox (NULL, szBuffer, szCaption, 0) ;

}
posted @ 2012-11-20 11:52  ilvu  阅读(302)  评论(0编辑  收藏  举报