Export (0) Print
Expand All
Expand Minimize

vsprintf, vswprintf

Write formatted output using a pointer to a list of arguments.

int vsprintf(
   char *buffer,
   const char *format,
   va_list argptr 
); 
int vswprintf(
   wchar_t *buffer,
   const wchar_t *format,
   va_list argptr 
);
int vswprintf(
   wchar_t *buffer,
   size_t count,
   const wchar_t *format,
   va_list argptr 
); //  (C++ only)

Parameters

buffer
Storage location for output.
count
Maximum number of characters to store
format
Format specification.
argptr
Pointer to list of arguments.

Return Value

vsprintf and vswprintf return the number of characters written, not including the terminating null character, or a negative value if an output error occurs.

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.

Security Note   There is no way to limit the number of characters written, which means that code using these functions is susceptible to buffer overruns. Use _vsnprintf instead, or call _vscprintf to determine how large a buffer is needed. Also, ensure that format is not a user-defined string. For more information, see Avoiding Buffer Overruns.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_vstprintf vsprintf vsprintf vswprintf

Requirements

Routine Required header Optional headers Compatibility
vsprintf <stdio.h> and <stdarg.h> <varargs.h>* ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
vswprintf <stdio.h> or <wchar.h>, and <stdarg.h> <varargs.h>* ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

* Required for UNIX V compatibility.

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_vsprintf.c
// This program uses vsprintf 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( buffer, format, args );
   printf( buffer );
   free( buffer );
}

int main( void )
{
   test( "%d %c %d\n", 123, '<', 456 );
   test( "%s\n", "This is a string" );
}

Output

123 < 456
This is a string

See Also

Stream I/O Routines | vprintf Functions Overview | Format Specification Fields: printf and wprintf Functions | fprintf | printf | sprintf | va_arg | Run-Time Routines and .NET Framework Equivalents

Show:
© 2014 Microsoft