Sprintf, swprintf

Source: Internet
Author: User

Write formatted data to a string.

int sprintf(   char *buffer,   const char *format [,   argument] ... );int swprintf(   wchar_t *buffer,   const wchar_t *format [,   argument] ... );swprintf(   wchar_t *buffer,   size_t count,   const wchar_t *format [,   argument]...);
Parameters
Buffer
Storage location for output
Count
Maximum number of characters to store
Format
Format-control string
Argument
Optional arguments

For more information, see format specifications.

Return Value

The number of characters written, or-1 if an error occurred.

SprintfReturns the number of bytes stored inBuffer,Not counting the terminating null character.SwprintfReturns the number of wide characters
Stored inBuffer,Not counting the terminating null wide character.

Remarks

TheSprintfFunction formats and stores a series of characters and values inBuffer. EachArgument(If any) is converted and output according to the corresponding
Format specification inFormat. The format consists of ordinary characters and has the same form and function asFormatArgument for printf.
A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

Security noteThere is no way to limit the number of characters written, which means that code usingSprintfIs susceptible to buffer overruns. Consider using
The related function _ snprintf, which specifies a maximum number of characters to be writtenBuffer,
Or use _ scprintf to determine how large a buffer is required. Also, ensure thatFormatIs
Not a user-defined string.

SwprintfIs a wide-character versionSprintf; The pointer argumentsSwprintfAre wide-character strings. Detection of encoding errors inSwprintfMay
Differ from that inSprintf.SwprintfAndFwprintfBehave identically else t thatSwprintfWrites
Output to a string rather than to a destination of TypeFile.

The iso c standard requires the following prototypeSwprintf:

int swprintf (wchar_t *, size_t, const wchar_t *, ...);

The prototype for _ snwprintf does match the standard. You may even want to do something like:

#define   swprintf   _snwprintf

Note that for C ++ users, one of the overload forms for swprintf has the same signature as the iso c standard requirementSwprintf.

Generic-text routine Mappings

Tchar. h routine _ Unicode & _ MBCS Not Defined _ MBCS defined _ Unicode defined
_ Stprintf Sprintf Sprintf Swprintf
Requirements
Routine Required Header Compatibility
Sprintf <Stdio. h> ANSI, WIN 98, win me, Win NT, Win 2000, Win XP
Swprintf <Stdio. h> or <wchar. h> ANSI, WIN 98, win me, Win NT, Win 2000, Win XP

For additional compatibility information, see compatibility in the introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_sprintf.c/* This program uses sprintf to format various * data and place them in the string named buffer. */#include <stdio.h>int main( void ){   char  buffer[200], s[] = "computer", c = 'l';   int   i = 35, j;   float fp = 1.7320534f;   /* Format and print various data: */   j  = sprintf( buffer,     "   String:    %s\n", s );   j += sprintf( buffer + j, "   Character: %c\n", c );   j += sprintf( buffer + j, "   Integer:   %d\n", i );   j += sprintf( buffer + j, "   Real:      %f\n", fp );   printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );}
Output

Output:   String:    computer   Character: l   Integer:   35   Real:      1.732053character count = 79
Example

// crt_swprintf.c// wide character example// also demonstrates swprintf returning error code#include <stdio.h>int main( void ){   wchar_t buf[100];   int len = swprintf( buf, L"%s", L"Hello world" );   printf( "wrote %d characters\n", len );   len = swprintf( buf, L"%s", L"Hello\xffff world" );   // swprintf fails because string contains WEOF (\xffff)   printf( "wrote %d characters\n", len );}
Output

  wrote 11 characters  wrote -1 characters
See also

Stream I/O routines | fprintf | printf | scanf | sscanf | vprintf
Functions | run-time routines and. NET Framework equivalents

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.