C programming string.h function - char *strncpy(char *dest, const char *src, size_t n)

The strncpy function from string.h library in C programming language is used to copy at most n characters from the source string src to the destination string dest. If the length of src is less than n, then the remaining characters in dest are filled with null characters.

The syntax for strncpy function is:

re‮ref‬ to:theitroad.com
char *strncpy(char *dest, const char *src, size_t n)

where:

  • dest: Pointer to the destination string where the content is to be copied.
  • src: Pointer to the source of data to be copied.
  • n: Maximum number of characters to be copied from source.

The function returns a pointer to the destination string dest.

Note that if the length of the src string is less than n, the remaining bytes in dest will be filled with null characters. If src string is longer than n, then only the first n characters will be copied to dest.