strncpy()

Copies a specified number of characters.

  #include <string.h>
  
  char *strcpy(char *dest, const char *source, size_t n);    
Parameter

dest

A pointer to a memory to copy the character string to.

source

A pointer to the character string to copy to dest.

n

The maximum number of characters to copy.

Remarks

The strncpy() function copies a maximum of n characters from the character array pointed to by source to the character array pointed to by dest. Neither dest nor source need necessarily point to null-terminated character arrays. This function has undefined behavior if dest and source overlap.

If a null character ( '\0') is reached in source before n characters have been copied, the function continues padding dest with null characters until n characters have been copied to dest.

The function does not terminate dest with a null character if n characters are copied fromsource before reaching a null character.

The function returns the value of dest.

This facility may not be available on some configurations of the EWL.

Listing: Example of strncpy() usage

#include <string.h>

#include <stdio.h>

#define MAX 50

int main(void)

{

char d[MAX];

char s[] = "123456789ABCDEFG";

strncpy(d, s, 9);

/* Terminate d explicitly in case strncpy() did not. */

d[MAX] = '\0';

puts(d);

return 0;

}

Output:

123456789