Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

strncpy - copy substring

&pagelevel(4)&pagelevel

Syntax

#include <string.h>

char *strncpy(char *s1, const char *s2, size_t n);

Description strncpy() copies a maximum of n characters from string s2 to string s1.

If string s2 contains less than n characters, only the length of s2 (strlen + 1) is copied, and
s1 is then padded to the length of n with null bytes.

If string s2 contains n or more characters (excluding the null byte), string s1 is not
automatically terminated with the null byte.

If string s1 contains more than n characters and the last character copied from s2 is not the
null byte, any data which may still remain in s1 is retained.

strncpy() does not automatically terminate s1 with the null byte.

Return val.

Notes

Pointer to the result string s1.

strncpy() does not verify whether s1 has enough space to accommodate the result!

Since strncpy() does not automatically terminate the result string with the null byte, it may
often be necessary to explicitly terminate s1 with a null byte. This is typically the case when
only a part of s2 is being copied, and s2 does not contain a null byte either.

The behavior is undefined if memory areas overlap.

See also

strcpy(), strlen(), string.h.