Share via


_snprintf, _snwprintf

Write formatted data to a string.

int_snprintf(char*buffer,size_tcount**,constchar*format [,**argument] ... );

int_snwprintf(wchar_t*buffer,size_tcount**,constwchar_t*format [,**argument] ... );

Routine Required Header Compatibility
_snprintf <stdio.h> Win 95, Win NT
_snwprintf <stdio.h> or <wchar.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned. _snwprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character. If the storage required to store the data exceeds count wide characters, then count wide characters are stored in buffer and a negative value is returned.

Parameters

buffer

Storage location for output

count

Maximum number of characters to store

format

Format-control string

argument

Optional arguments

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

_snwprintf is a wide-character version of _snprintf; the pointer arguments to _snwprintf are wide-character strings. Detection of encoding errors in _snwprintf may differ from that in _snprintf. _snwprintf, like swprintf, writes output to a string rather than to a destination of type FILE.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_sntprintf _snprintf _snprintf _snwprintf

Example

/* SPRINTF.C: This program uses sprintf to format various
 * data and place them in the string named buffer.
 */

#include <stdio.h>

void 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,     "\tString:    %s\n", s );
   j += sprintf( buffer + j, "\tCharacter: %c\n", c );
   j += sprintf( buffer + j, "\tInteger:   %d\n", i );
   j += sprintf( buffer + j, "\tReal:      %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 = 71

Stream I/O Routines

See Also   sprintf, fprintf, printf, scanf, sscanf, vprintf Functions