Compartilhar via


vsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at vsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l.

Write formatted output using a pointer to a list of arguments. These are versions of vsprintf, _vsprintf_l, vswprintf, _vswprintf_l, __vswprintf_l with security enhancements as described in Security Features in the CRT.

Syntax

int vsprintf_s(  
   char *buffer,  
   size_t numberOfElements,  
   const char *format,  
   va_list argptr   
);   
int _vsprintf_s_l(  
   char *buffer,  
   size_t numberOfElements,  
   const char *format,  
   locale_t locale,  
   va_list argptr   
);   
int vswprintf_s(  
   wchar_t *buffer,  
   size_t numberOfElements,  
   const wchar_t *format,  
   va_list argptr   
);  
int _vswprintf_s_l(  
   wchar_t *buffer,  
   size_t numberOfElements,  
   const wchar_t *format,  
   locale_t locale,  
   va_list argptr   
);  
template <size_t size>  
int vsprintf_s(  
   char (&buffer)[size],  
   const char *format,  
   va_list argptr   
); // C++ only  
template <size_t size>  
int vswprintf_s(  
   wchar_t (&buffer)[size],  
   const wchar_t *format,  
   va_list argptr   
); // C++ only  

Parameters

buffer
Storage location for output.

numberOfElements
Size of buffer in characters.

format
Format specification.

argptr
Pointer to list of arguments.

locale
The locale to use.

Return Value

vsprintf_s and vswprintf_s return the number of characters written, not including the terminating null character, or a negative value if an output error occurs. If buffer or format is a null pointer, if count is zero, or if the format string contains invalid formatting characters, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, the functions return -1 and set errno to EINVAL.

For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

Remarks

Each of these functions takes a pointer to an argument list, and then formats and writes the given data to the memory pointed to by buffer.

vswprintf_s conforms to the ISO C Standard for vswprintf, which requires the second parameter, count, of type size_t.

These functions differ from the non-secure versions only in that the secure versions support positional parameters. For more information, see printf_p Positional Parameters.

The versions of these functions with the _l suffix are identical except 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.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_vstprintf_s vsprintf_s vsprintf_s vswprintf_s
_vstprintf_s_l _vsprintf_s_l _vsprintf_s_l _vswprintf_s_l

Requirements

Routine Required header Optional headers
vsprintf_s, _vsprintf_s_l <stdio.h> and <stdarg.h> <varargs.h>*
vswprintf_s, _vswprintf_s_l <stdio.h> or <wchar.h>, and <stdarg.h> <varargs.h>*

* Required for UNIX V compatibility.

For additional compatibility information, see Compatibility in the Introduction.

Example

// crt_vsprintf_s.c  
// This program uses vsprintf_s to write to a buffer.  
// The size of the buffer is determined by _vscprintf.  
  
#include <stdlib.h>  
#include <stdarg.h>  
  
void test( char * format, ... )  
{  
   va_list args;  
   int len;  
   char * buffer;  
  
   va_start( args, format );  
   len = _vscprintf( format, args ) // _vscprintf doesn't count  
                               + 1; // terminating '\0'  
   buffer = malloc( len * sizeof(char) );  
   vsprintf_s( buffer, len, format, args );  
   puts( buffer );  
   free( buffer );  
}  
  
int main( void )  
{  
   test( "%d %c %d", 123, '<', 456 );  
   test( "%s", "This is a string" );  
}  
123 < 456  
This is a string  

.NET Framework Equivalent

System::String::Format

See Also

Stream I/O
vprintf Functions
Format Specification Syntax: printf and wprintf Functions
fprintf, _fprintf_l, fwprintf, _fwprintf_l
printf, _printf_l, wprintf, _wprintf_l
sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
va_arg, va_copy, va_end, va_start