Pointer to the destination buffer where the source string will be copied.
2nd Parameter Type : const char *
Pointer to read-only string
2nd Parameter
Pointer to the null-terminated source string to be copied.
3rd Parameter Type : size_t
Platform-specific unsigned type for array indices and memory sizes.
3rd Parameter
The maximum number of characters to copy from the source string, including the null terminator if present.
Read more about parameters of strncpy in parameters section
The strncpyfunction in C language Copies a specified number of characters from the source string to the destination string.
The strncpy function copies up to n characters from the string pointed to by src to the buffer pointed to by dest. If the length of src is less than n, the remainder of dest will be padded with null bytes. If the length of src is greater than or equal to n, dest may not be null-terminated.
The strncpyfunction takes 3
parameters:
•
char * `dest`: Pointer to the destination buffer where the source string will be copied.
•
const char * `src`: Pointer to the null-terminated source string to be copied.
•
size_t `n`: The maximum number of characters to copy from the source string, including the null terminator if present.
Copies up to `n` characters from the source string `src` to the destination buffer `dest`. If `src` is shorter than `n`, the remaining space in `dest` is padded with null characters. If `src` is longer than `n`, the result will not be null-terminated. Returns a pointer to the destination string `dest`.
The strncpy function return value :
Returns a pointer to the destination string dest
Output
This example demonstrates basic usage of strncpy to copy the first 5 characters of a string.