Export (0) Print
Expand All
Expand Minimize

sprintf, swprintf

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.

sprintf returns the number of bytes stored in buffer, not counting the terminating null character. swprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character.

Remarks

The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

Security Note   There is no way to limit the number of characters written, which means that code using sprintf is susceptible to buffer overruns. Consider using the related function _snprintf, which specifies a maximum number of characters to be written to buffer, or use _scprintf to determine how large a buffer is required. Also, ensure that format is not a user-defined string.

swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Detection of encoding errors in swprintf may differ from that in sprintf. swprintf and fwprintf behave identically except that swprintf writes output to a string rather than to a destination of type FILE.

The ISO C Standard requires the following prototype for swprintf:

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 requirement for swprintf.

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.732053

character 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

Show:
© 2014 Microsoft