strncpy, wcsncpy, _mbsncpy

Copy characters of one string to another.

char*strncpy(char*strDest,constchar*strSource,size_tcount**);**

wchar_t*wcsncpy(wchar_t*strDest,constwchar_t*strSource,size_tcount**);**

unsignedchar*_mbsncpy(unsignedchar*strDest,constunsignedchar*strSource,size_tcount**);**

Routine Required Header Compatibility
strncpy <string.h> ANSI, Win 95, Win NT
wcsncpy <string.h> or <wchar.h> ANSI, Win 95, Win NT
_mbsncpy <mbstring.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

Each of these functions returns strDest. No return value is reserved to indicate an error.

Parameters

strDest

Destination string

strSource

Source string

count

Number of characters to be copied

Remarks

The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.

wcsncpy and _mbsncpy are wide-character and multibyte-character versions of strncpy. The arguments and return value of wcsncpy and _mbsncpy vary accordingly. These three functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcsncpy strncpy _mbsnbcpy wcsncpy

Example

/* STRNCPY.C */

#include <string.h>
#include <stdio.h>

void main( void )
{
   char string[100] = "Cats are nice usually";
   printf ( "Before: %s\n", string );
   strncpy( string, "Dogs", 4 );
   strncpy( string + 9, "mean", 4 );
   printf ( "After:  %s\n", string );
}

Output

Before: Cats are nice usually
After:  Dogs are mean usually

String Manipulation Routines

See Also   _mbsnbcpy, strcat, strcmp, strcpy, strncat, strncmp, _strnicmp, strrchr, _strset, strspn