Visual Studio 2005Other Versions
- Visual Studio 2010
- Visual Studio 2008
Write formatted data to a string. These are versions of sprintf, _ sprintf_l, swprintf, _ swprintf_l, _ swprintf_l with security enhancements as described in Security Enhancements in the CRT.
Copy
int sprintf_s( char *buffer, size_t sizeOfBuffer, const char *format [, argument] ... );int _sprintf_s_l( char *buffer, size_t sizeOfBuffer, const char *format, locale_t locale [, argument] ... );int swprintf_s( wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format [, argument]...);int _swprintf_s_l( wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, locale_t locale [, argument]…);template <size_t size>int sprintf_s( char (&buffer)[size], const char *format [, argument] ... ); // C++ onlytemplate <size_t size>int swprintf_s( wchar_t (&buffer)[size], const wchar_t *format [, argument]...); // C++ only
Parameters
-
Buffer
-
Storage location for output
-
SizeOfBuffer
-
Maximum number of characters to store.
-
Format
-
Format-control string
-
Argument
-
Optional arguments
-
Locale
-
The locale to use.
For more information, see Format Specifications.
Return Value
The number of characters written, or-1 if an error occurred. IfBufferOrFormatIs a null pointer,Sprintf_sAndSwprintf_sReturn-1 and setErrnoToEINVAL.
Sprintf_sReturns the number of bytes stored inBuffer, Not counting the terminating null character.Swprintf_sReturns the number of wide characters stored inBuffer, Not counting the terminating null wide character.
Remarks
TheSprintf_sFunction 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.
One main differenceSprintf_sAndSprintfIs thatSprintf_sChecks the format string for valid formatting characters, whereasSprintfOnly checks if the format string or buffer areNULLPointers. If either check fails, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, the function returns-1 and setsErrnoToEINVAL.
The other main differenceSprintf_sAndSprintfIs thatSprintf_sTakes a length parameter specifying the size of the output buffer in characters. if the buffer is too small for the text being printed then the buffer is set to an empty string and the invalid parameter handler is invoked. unlikeSnprintf,Sprintf_sGuarantees that the buffer will be null-terminated (unless the buffer size is zero ).
Swprintf_sIs a wide-character versionSprintf_s; The pointer argumentsSwprintf_sAre wide-character strings. Detection of encoding errors inSwprintf_sMay differ from that inSprintf_s. The versions of these functions with_ LSuffix are identical values t that they use the locale parameter passed in instead of the current thread locale.
In C ++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. for more information, see Secure Template Overloads.
There are versionsSprintf_sThat offer additional control over what happens if the buffer is too small. For more information, see _ snprintf_s, _ snprintf_s_l, _ snwprintf_s, _ snwprintf_s_l.
Generic-Text Routine Mappings
| TCHAR. H routine |
_ UNICODE & _ MBCS not defined |
_ MBCS defined |
_ UNICODE defined |
_ Stprintf_s |
Sprintf_s |
Sprintf_s |
Swprintf_s |
_ Stprintf_s_l |
_ Sprintf_s_l |
_ Sprintf_s_l |
_ Swprintf_s_l |
Requirements
| Routine |
Required header |
Compatibility |
Sprintf_s,_ Sprintf_s_l |
<Stdio. h> |
Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
Swprintf_s,_ Swprintf_s_l |
<Stdio. h> or <wchar. h> |
Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
For additional compatibility information, see Compatibility in the Introduction.
Example
Copy
// crt_sprintf_s.c// This program uses sprintf_s 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_s( buffer, 200, " String: %s\n", s ); j += sprintf_s( buffer + j, 200 - j, " Character: %c\n", c ); j += sprintf_s( buffer + j, 200 - j, " Integer: %d\n", i ); j += sprintf_s( buffer + j, 200 - j, " Real: %f\n", fp ); printf_s( "Output:\n%s\ncharacter count = %d\n", buffer, j );}Output
Output: String: computer Character: l Integer: 35 Real: 1.732053character count = 79
Copy
// crt_swprintf_s.c// wide character example// also demonstrates swprintf_s returning error code#include <stdio.h>int main( void ){ wchar_t buf[100]; int len = swprintf_s( buf, 100, L"%s", L"Hello world" ); printf( "wrote %d characters\n", len ); len = swprintf_s( buf, 100, L"%s", L"Hello\xffff world" ); // swprintf_s fails because string contains WEOF (\xffff) printf( "wrote %d characters\n", len );}Output
wrote 11 characterswrote -1 characters