_strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_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 _strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_l.

Initializes characters of a string to a given character. More secure versions of these functions exist; see _strnset_s, _strnset_s_l, _wcsnset_s, _wcsnset_s_l, _mbsnset_s, _mbsnset_s_l.


_mbsnset and _mbsnset_l cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW.


char *_strnset(  
   char *str,  
   int c,  
   size_t count   
char *_strnset_l(  
   char *str,  
   int c,  
   size_t count,  
   locale_t locale  
wchar_t *_wcsnset(  
   wchar_t *str,  
   wchar_t c,  
   size_t count   
wchar_t *_wcsnset_l(  
   wchar_t *str,  
   wchar_t c,  
   size_t count,  
   _locale_t locale  
unsigned char *_mbsnset(  
   unsigned char *str,  
   unsigned int c,  
   size_t count   
unsigned char *_mbsnset_l(  
   unsigned char *str,  
   unsigned int c,  
   size_t count,  
   _locale_t locale  


String to be altered.

Character setting.

Number of characters to be set.

Locale to use.

Return Value

Returns a pointer to the altered string.


The _strnset function sets, at most, the first count characters of str to c (converted to char). If count is greater than the length of str, the length of str is used instead of count.

_wcsnset and _mbsnset are wide-character and multibyte-character versions of _strnset. The string arguments and return value of _wcsnset are wide-character strings; those of _mbsnset are multibyte-character strings. These three functions behave identically otherwise.

_mbsnset validates its parameters; if str is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation . If execution is allowed to continue, _mbsnset returns NULL and sets errno to EINVAL. _strnset and _wcsnset do not validate their parameters.

The output value is affected by the setting of the LC_CTYPE category setting of the locale; see setlocale for more information. The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the_l suffix are identical except that they use the locale parameter passed in instead. For more information, see Locale.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tcsnset _strnset _mbsnbset _wcsnset
_tcsnset_l _strnset_l _mbsnbset_l _wcsnset_l


Routine Required header
_strnset <string.h>
_strnset_l <tchar.h>
_wcsnset <string.h> or <wchar.h>
_wcsnset_l <tchar.h>
_mbsnset, _mbsnset_l <mbstring.h>

For additional compatibility information, see Compatibility.


// crt_strnset.c  
// compile with: /W3  
#include <string.h>  
#include <stdio.h>  
int main( void )  
   char string[15] = "This is a test";  
   /* Set not more than 4 characters of string to be *'s */  
   printf( "Before: %s\n", string );  
   _strnset( string, '*', 4 ); // C4996  
   // Note: _strnset is deprecated; consider using _strnset_s  
   printf( "After:  %s\n", string );  
Before: This is a test  
After:  **** is a test  

.NET Framework Equivalent


See Also

String Manipulation
Interpretation of Multibyte-Character Sequences
strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strcpy, wcscpy, _mbscpy
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l