在学习C语言的时候,经常把变量的值或者其他提示信息用printf函数输出。但是在Windows编程中,没有了格式化输入、输出语句,对于刚开始学Windows编程的人来说,怎么把变量或者其他信息输出到屏幕上成了一个难题。在《programing Windows》中有关于在Windows中使用printf的介绍,跟大家分享一下。
In Windows programs, you can still display text by using sprintfand other functions in the sprintffamily. These functions work just like printf, except that they write the formatted output to a character string buffer that you provide as the function's first argument. You can then do what you want with this character string(such as pass it to MessageBox).
The sprintf function is defined like this:
int printf (char * szBuffer, const char * szFormat, ...);
The first argument is a character buffer; this si followed by the formatting string. Rather than writing the formatted result in standard output,
sprintfstores it in
szBuffer. The function returns the length of the string. In character-mode programming,
printf ("The sum of %i and %i is %i", 5, 3, 5=3);
is functionally equivalent to
char szBuffer[1024];
sprintf (szBuffer, "The sum of %i and &i is %i", 5, 3, 5+3);
puts (szBuffer);
In Windows, you can use
MessageBoxrather than puts to display the results.
Be careful, the character buffer you define must be large enough for the result. A Microsoft-specific function named _snprintfsolves this problem by introducing another argument that indicates(指示) the size ofthe buffer in characters.
A variation of sprintfis vsprintf, which has only three arguments. The vsprintffunction is used to implement(实现) a function of your own that must perform printf-like formatting of a variable number of arguments. The first two arguments of vsprintf are the same as sprintf:the character buffer for storing the result and the formatting string. The thirs argument is a pointer to an array of arguments to be formatted. In practice(实际上), this pointer actually references variables that have been stored on the stack in preparation for a function call. The va_list, va_start,and va_end macros(defined in STDARG.H) help in working with this stack pointer.
Of course, with the introduction of wide characters, the sprintf functions blossomed in number, creating a thoroughly confusing jumble(混乱的) of function names. here's a chart that shows all the sprintf functions supported by Microsoft's C run-time library and by Windows.
In the wide-character versions of the sprintf functions, the string buffer is defined as a wide-character string. In the wide-character versions of all these functions, the formatting string must be a wide-character string . However, it is up to you to make sure that any other strings you pass to these functions are also composed(由..组成) of wide characters.
Here is an example which shows how to implement a MessageBoxprintf function that takes a variable number of arguments and formats them like print.
/*
* MessageBoxPrintf.c -- Display in a message box
*/
#include
#include
#include
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 argumens
_vsntprintf(szBuffer, sizeof(szBuffer)/sizeof (TCHAR),
szFormat, pArgList);
// The va_end macro just zeros out pArgList for no good reason
va_end(pArgList);
return MessageBox(NULL, szBuffer, szCaption, 0);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
int i;
int j;
i = 3;
j = 5;
MessageBoxPrintf(TEXT("Sum"), TEXT("The sum of %i and %i is %i"), i, j, i+j);
return 0;
} 关于va_list, va_start, va_end, 在以后的blog里再讨论。