The _snprintf_s function formats and stores count or fewer characters in buffer and appends a terminating null. Each argument (if any) is converted and output according to the corresponding format specification in format. The formatting is consistent with the printf family of functions; see Format Specification Fields: printf and wprintf Functions. If copying occurs between strings that overlap, the behavior is undefined.
If count is _TRUNCATE, then _snprintf_s writes as much of the string as will fit in buffer while leaving room for a terminating null. If the entire string (with terminating null) fits in buffer, then _snprintf_s returns the number of characters written (not including the terminating null); otherwise, _snprintf_s returns -1 to indicate that truncation occurred.
Security Note |
|---|
| Ensure that format is not a user-defined string. |
_snwprintf_s is a wide-character version of _snprintf_s; the pointer arguments to _snwprintf_s are wide-character strings. Detection of encoding errors in _snwprintf_s might differ from that in _snprintf_s. _snwprintf_s, like swprintf_s, writes output to a string rather than to a destination of type FILE.
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 and _MBCS not defined
|
_MBCS defined
|
_UNICODE defined
|
| _sntprintf_s | _snprintf_s | _snprintf_s | _snwprintf_s |
| _sntprintf_s_l | _snprintf_s_l | _snprintf_s_l | _snwprintf_s_l |