In Windows, programmers who use printf in the text mode and the C language programming history in the Command column often prefer the printf function. Even though simple commands (such as puts) can be used, it is not surprising that printf appears in the "Hello, world" program of kernighan and Ritchie. We know that the enhanced "Hello, world" still requires the formatting output of printf, so we 'd better use it from the beginning. But there is a bad message: printf cannot be used in Windows programs. Although Windows programs can use the link library for most C execution periods-in fact, many program writers prefer to use C memory management and file I/O functions instead of equivalent functions in Windows-Windows has no concept of standard input and standard output. In Windows, you can use fprintf instead of printf. The good news is that other functions in the sprintf and sprintf series can still be used to display text. Apart from formatting the content and outputting it to the string buffer provided by the first parameter of the function, these functions have the same function as printfi. Then you can operate the string (for example, pass it to MessageBox ). If you have never used sprintf (I didn't use this function when I first started writing a Windows program), here is a brief execution entity. The printf function is described as follows: int printf (const char * szformat ,...); the first parameter is a format string followed by multiple parameters of different types corresponding to the Code in the format string. The sprintf function is defined as follows: int sprintf (char * szbuffer, const char * szformat,...); the first parameter is the character buffer; followed by a format string. Sprintf saves the formatted result to szbuffer instead of standard output. This function returns the length of the string. In the text mode program design, printf ("the sum of % I and % I is % I", 5, 3, 5 + 3 ); similar to Char szbuffer [100]; sprintf (szbuffer, "the sum of % I and % I is % I", 5, 3, 5 + 3 ); puts (szbuffer); in windows, MessageBox is used to display better results than puts. Almost everyone has experienced that when the format string is different from the formatted variable, the printf execution error may occur and the program may be crashed. When using sprintf, you not only need to worry about this, but also have a new burden: the string buffer you define must be large enough to store the results. Microsoft special function _ snprintf solves this problem. This function introduces another parameter, indicating the buffer size calculated by characters. Vsprintf is a deformation of sprintf. It has only three parameters. Vsprintf is used to execute a custom function with multiple parameters, similar to the printf format. The first two parameters of vsprintf are the same as those of sprintf: A character buffer for storing results and a format string. The third parameter is a pointer to the formatted parameter array. In fact, this pointer points to the variable in the stack for function call. Va_list, va_start, and va_end macros (defined in stdarg. h) Help us process stack pointers. The scrnsize program at the end of this chapter shows how to use these macros. Using the vsprintf function, the sprintf function can be written as follows: int sprintf (char * szbuffer, const char * szformat ,...) {int ireturn; va_list pargs; va_start (pargs, szformat); ireturn = vsprintf (szbuffer, szformat, pargs); va_end (pargs); Return ireturn ;} the va_start macro sets parg to point to a stack variable. The variable address is above the stack parameter szformat. Since many early Windows programs used sprintf and vsprintf, Microsoft added two similar functions to Windows APIs. Windows's wsprintf and wvsprintf functions are functionally the same as sprintf and vsprintf, but they cannot process floating point formats. Of course, with the publication of wide characters, the sprintf type functions have increased a lot, making the function names extremely messy. Table 2-1 lists Microsoft's C-execution linked libraries and all sprintf functions supported by windows. Table 2-1 Number of variables for common ASCII characters standard version sprintf swprintf _ stprintf max length _ snprintf _ snwprintf _ sntprintf windows wsprintfa wsprintfW wsprintf parameter array pointer standard version vswprintf _ vstprintf max-length version _ vsnprintf _ vsnwprintf _ vsntprintf Windows Version wvsprintfa wvsprintfw wvsprintf in the wide character version sprintf function, define the string buffer as a wide string. Among all the functions of the wide character version, the format string must be a wide string. However, you must ensure that other strings passed to these functions must also contain wide characters.