Copies the specified number of characters from one string to another.
#include <string.h> char *strncpy(char *string1, const char *string2, size_t count);
errno_t strncpy_s(char *string1, rsize_t s1size, const char *string2, rsize_t count);
#include <wchar.h>
wchar_t *wcsncpy(wchar_t *string1, const wchar_t *string2, size_t count);
errno_t wcsncpy_s(wchar_t *string1, rsize_t s1size, const wchar_t *string2, rsize_t count);
string1
s1max
string2
count
Copies count characters of string2
to string1
.
If count is less than the length of string2
, a null character is not appended automatically to the copied string. If count is greater than the length of string2
, the string1
result is padded with null characters up to length count.
The behavior of strncpy is undefined if the address ranges of the source and destination strings overlap.
The functions strncpy_s and wcsncpy_s behave differently:
These functions try to copy the first D characters of string2 to string1, where D is the lesser of count and the length of string2. If those D characters will fit within string1 (whose size is given as s1size) and still leave room for a null terminator, then those characters are copied and a terminating null is appended; otherwise, string1[0] is set to the null character.
strncpy and wcsncpy: returns string1
.
strncpy_s and wcsncpy_s: returns 0 on success, otherwise a non-zero value is returned indicating the error status.
tchar.h routine |
_UNICODE not defined |
_UNICODE defined |
---|---|---|
_tcsncpy | strncpy | _wcsncpy |
_tcsncpy_s | strncpy_s | _wcsncpy_s |
Versions | Defined in | Include | Link to |
---|---|---|---|
INtime 3.0 INtime 6.0 (for wide-character and generic text versions) |
intime/rt/include/string.h intime/rt/include/wchar.h intime/rt/include/tchar.h |
string.h wchar.t tchar.h |
clib.lib |