Variable Parameter programming for vsprintf, vswprintf, and printf Functions

Source: Internet
Author: User

In C language programming, we inevitably need to access variable parameter functions. For C languages that do not support function polymorphism, using variable parameters and macro-defined functions is a good way to implement function polymorphism. Before proceeding to variable parameter functions, let's take a look at two typical variables: vsprintf and sprintf.

I. vsprintf Functions

Header file

Stdio. h

Category

Memory and string manipulation routines

Prototype

Int vsprintf (char * buffer, const char * format, va_list Arglist );

Int vswprintf (wchar_t * buffer, const wchar_t * format, va_list Arglist );

Description

Writes formatted output to a string.

The V... printf functions are known as alternate entry points for... printf functions. they behave exactly like their... printf counterparts, but they accept a pointer to a list of arguments instead of an argument list.

Vsprintf accepts a pointer to a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string. there must be the same number of format specifiers as arguments.

Return Value

Vsprintf returns the number of bytes output. in the event of error, vsprintf returns EOF.

-- Comparison Translation

Header file
Stdio. h

Category
Memory and string operations

Function prototype
Int vsprintf (char * buffer, const char * format, va_list Arglist );

Int vswprintf (wchar_t * buffer, const wchar_t * format, va_list Arglist );

Description
Write formatted output to a string

V .. printf function family is an alternative to the .. print function family. They operate like the .. printf function family, But they accept pointers to the parameter list rather than the parameter list.
Vsprintf accepts a pointer to a series of variable parameters, provides each parameter with a formatting definition included in form, and outputs formatted data to a string, the number of format definitions and parameters must be equal.

Return Value
Vsprintf returns the number of output bytes. If an error occurs, EOF is returned.

Ii. sprintf Functions

Header file

Stdio. h

Category

Memory and string manipulation routines

Prototype

Int sprintf (char * buffer, const char * Format [, argument,...]);

Int swprintf (wchar_t * buffer, const wchar_t * Format [, argument,...]);

Description

Writes formatted output to a string.

Note: For details on format specifiers, see printf.

Sprintf accepts a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string.

Sprintf applies the first format specifier to the first argument, the second to the second, and so on. There must be the same number of format specifiers as arguments.

Return Value

On success, sprintf returns the number of bytes output. The return value does not include the terminating null byte in the count.

On error, sprintf returns EOF.

-- Comparison Translation

Header file:

Stdio. h

Header file
Stdio. h

Category
Memory and string operations

Function prototype
Int sprintf (char * buffer, const char * Format [, argument,...]);

Int swprintf (wchar_t * buffer, const wchar_t * Format [, argument,...]);

Description
Write formatted output to a string
Note: For formatting definition specifications, see printf
Sprintf accepts a series of parameters, provides a formatting definition for each parameter, and outputs formatted data to a string
Sprintf provides the first formatting definition for the first parameter and the second formatting definition. The number of formatting definitions must be consistent with the number of parameters.

Return Value
Success: the number of output bytes. The returned value does not include the number of bytes that terminate NULL bytes.
Error. EOF is returned.

To facilitate the use of these two functions, the following is a program snippet:

Char szbuffer [256];
Sprintf (szbuffer, "Welcome % d, % s", 1, "Hi ");
Showmessage (szbuffer );
Vsprintf (szbuffer, "Welcome % d, % s", 1, "Hi"); // <-[C ++ Error] unit1.cpp (24 ): e2034 cannot convert 'int' to 'void *'
Showmessage (szbuffer );

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/9527/archive/2008/05/19/2457816.aspx

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.