printf,sprintf,vsprintf difference "Turn"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/anye3000/article/details/6593551

Programmers with a history of C writing tend to like the printf function in particular. Even though simpler commands (such as puts) can be used, printf appears in the "hello of Kernighan and Ritchie, and the world" program is not surprising at all. We know that after the enhanced "hello, world" will eventually need the formatted output of printf, so we'd better use it from the beginning.

But there's a bad news: you can't use printf in a Windows program. Although most C's execution-time function libraries can be used in Windows programs. In fact, many programmers prefer to use C's memory management and I/O functions rather than the equivalent function-windows in Windows without the concept of standard input and standard output. You can use fprintf instead of printf in Windows programs.

The good news is that you can still use other functions in the sprintf and sprintf series to display text. These functions have the same functionality as printf except that the content is formatted to output to the string buffer provided by the first argument. You can then manipulate the string (for example, pass it to the MessageBox).

If you've never used sprintf (I didn't use this function the first time I started writing Windows programs), here's a short execution entity.

The printf function is described below:

int printf (const char * szformat, ...);
The first parameter is a formatted string followed by multiple parameters of different types that correspond to the code in the formatted string.

The sprintf function is defined as follows:

int sprintf (char * szbuffer, const char * szformat, ...);
The first argument is a string buffer, followed by a format word string. Instead of sprintf the formatted result standard output, it will be stored in szbuffer. The function returns the length of the string.

In the command-line program,

printf ("The sum of%i and%i is%i", 5, 3, 5+3);
function is the same as

Char szbuffer [100];
sprintf (Szbuffer, "The sum of%i and%i is%i", 5, 3, 5+3);
Puts (szbuffer);
In Windows, displaying results using the MessageBox is better than puts.

Almost everyone has experienced that when formatting a string is not the same as the formatted variable, it can cause printf to execute an error and possibly cause the program to fall out. When using sprintf, you not only have to worry about these, but there is a new burden: The string buffers you define must be large enough to hold the results. The Microsoft dedicated function _snprintf solves this problem by introducing another parameter that represents the buffer size computed in bytes.

The vsprintf function is defined as follows:

int vsprintf (char *string, char *format, va_list param);

Vsprintf is a variant of the sprintf, which has only three parameters. Vsprintf is used to perform functions with an indefinite number of parameters, similar to the printf format. The first two parameters of vsprintf are the same as sprintf: a string buffer for saving results and a formatted string. The third parameter is a pointer to the format parameter queue. In effect, the pointer points to a variable in the stack for a function call. Va_list, Va_start, and Va_end macros (defined in STDARG.H) help us with 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 like this:

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 that is addressed above the stack parameter szformat.

Because many early Windows programs used sprintf and vsprintf, Microsoft eventually added two similar functions to the Windows API. Windows wsprintf and WVSPRINTF functions are functionally the same as sprintf and vsprintf, but they cannot handle floating-point formatting.

Of course, with the publication of wide characters, the functions of the sprintf type increase a lot, making the function name extremely confusing. The following is a list of Microsoft's C execution time libraries and all the sprintf functions supported by Windows.

ASCII Wide character General
Variable number of arguments
Standard Version sprintf swprintf _stprintf
Max length version _snprintf _snwprintf _sntprintf
Windows Edition WSPRINTFA WSPRINTFW wsprintf


Pointer to parameter queue
Standard Version vsprintf vswprintf _vstprintf
Max length version _vsnprintf _vsnwprintf _vsntprintf
Windows Edition WVSPRINTFA WVSPRINTFW wvsprintf

In the wide-character version of the sprintf function, the string buffer is defined as a wide string. In all these functions of the wide-character version, the formatted string must be a wide string. However, you must ensure that the other strings passed to these functions must also consist of wide characters.

printf,sprintf,vsprintf difference "Turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.