C Library Function - strncpy()
Description
C Library Function **char *strncpy(char dest, const char src, size_t n) Puts the src copies the string pointed to by dest, copying at most n characters. When the length of src is less than n, the remainder of dest will be padded with null bytes.
Declaration
Below is the declaration of the strncpy() function.
char *strncpy(char *dest, const char *src, size_t n)
Parameters
- dest -- points to the destination array used to store the copied content.
- src -- the string to copy.
- n -- the number of characters to copy from the source.
Return Value
This function returns the final copied string.
Example
The following example demonstrates the use of the strncpy() function. Here, we use the function memset() to clear memory locations.
Example
#include
<
stdio.h
#include
<
string.h
int
main
(
)
{
char
src
[
40
];
char
dest
[
12
]
;
memset
(
dest
, '\
0
',
sizeof
(
dest
)
)
;
strcpy
(
src
,
"
This is runoob.com
"
)
;
strncpy
(
dest
,
src
,
10
)
;
printf
(
"
Final destination string: %s
\
n
"
,
dest
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
最终的目标字符串: This is ru
C Standard Library -