If you want to copy 25 characters from source string using strncpy( ) function, only 20 characters from source string will be copied into destination string and remaining 5 characters won’t be copied and will be truncated. Example program for strncpy( ) function in C.
C Strncpy
- The C Standard Library
- C Standard Library Resources
- C Programming Resources
- Selected Reading
Description
The C library function char *strncpy(char *dest, const char *src, size_t n) copies up to n characters from the string pointed to, by src to dest. In a case where the length of src is less than that of n, the remainder of dest will be padded with null bytes.
Declaration
Linux Strncpy
Following is the declaration for strncpy() function.
Parameters
- dest − This is the pointer to the destination array where the content is to be copied.
- src − This is the string to be copied.
- n − The number of characters to be copied from source.
Return Value
This function returns the final copy of the copied string.
Example
The following example shows the usage of strncpy() function. Gross beat vst crack. Here we have used function memset() to clear the memory location.
Let us compile and run the above program that will produce the following result −
string_h.htm