_mbsnbset, _mbsnbset_l

Sets the first n bytes of a multibyte-character string to a specified character. More secure versions of these functions are available; see _mbsnbset_s, _mbsnbset_s_l.

Important note Important

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

unsigned char *_mbsnbset(
   unsigned char *str,
   unsigned int c,
   size_t count 
unsigned char *_mbsnbset_l(
   unsigned char *str,
   unsigned int c,
   size_t count,
   _locale_t locale


String to be altered.


Single-byte or multibyte-character setting.


Number of bytes to be set.


Locale to use.

_mbsnbset returns a pointer to the altered string.

The _mbsnbset and _mbsnbset_l functions set, at most, the first count bytes of str to c. If count is greater than the length of str, the length of str is used instead of count. If c is a multibyte character and cannot be set entirely into the last byte specified by count, the last byte is padded with a blank character. _mbsnbset and _mbsnbset_ldoes not place a terminating null at the end of str.

_mbsnbset and _mbsnbset_lis similar to _mbsnset, except that it sets count bytes rather than count characters of c.

If str is NULL or count is zero, this function generates an invalid parameter exception as described in Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and the function returns NULL. Also, if c is not a valid multibyte character, errno is set to EINVAL and a space is used instead.

The output value is affected by the setting of the LC_CTYPE category setting of the locale; see setlocale for more information. The _mbsnbset version of this function uses the current locale for this locale-dependent behavior; the _mbsnbset_l version is identical except that it use the locale parameter passed in instead. For more information, see Locale.

Security Note   This API incurs a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer Overruns.

Generic-Text Routine Mappings

Tchar.h routine

_UNICODE and _MBCS not defined

_MBCS defined

_UNICODE defined










Required header





For more compatibility information, see Compatibility.

// crt_mbsnbset.c
// compile with: /W3
#include <mbstring.h>
#include <stdio.h>

int main( void )
   char string[15] = "This is a test";
   /* Set not more than 4 bytes of string to be *'s */
   printf( "Before: %s\n", string );
   _mbsnbset( string, '*', 4 ); // C4996
   // Note; _mbsnbset is deprecated; consider _mbsnbset_s
   printf( "After:  %s\n", string );

Before: This is a test
After:  **** is a test

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.