May 6, 2002 STRCPY 3

### NAME

strcpy, strncpy - copy strings

### LIBRARY

Standard C Library (libc, -lc)

### SYNOPSIS

```char``*` strcpy(```char * restrict dst``, const char * restrict src`)
```char``*` strncpy(```char * restrict dst``, const char * restrict src``, size_t len`)

### DESCRIPTION

The strcpy(``) and strncpy(``) functions copy the string ```src` to ```dst` (including the terminating `\0' character).

The strncpy(``) function copies not more than ```len` characters into ```dst`, appending `\0' characters if ```src` is less than ```len` characters long, and not terminating ```dst` if ```src` is ```len` or more characters long.

### RETURN VALUES

The strcpy(``) and strncpy(``) functions return ```dst`.

### EXAMPLES

The following sets `````chararray`'' to `````abc\0\0\0`''.

  `

      (void)strncpy(chararray, "abc", 6);

  `

The following sets `````chararray`'' to `````abcdef`'' and does not nul-terminate chararray because the source string is >= the length parameter. strncpy(``) only nul-terminates the destination string when the length of the source string is less than the length parameter.

  `

      (void)strncpy(chararray, "abcdefgh", 6);

  `

The following copies as many characters from input to buf as will fit and nul-terminates the result. Because strncpy(``) does not guarantee to nul-terminate the string itself, we must do this by hand.

  `

      char buf[BUFSIZ];

      (void)strncpy(buf, input, sizeof(buf) - 1);
      buf[sizeof(buf) - 1] = '\0';

  `

Note that strlcpy(3) is a better choice for this kind of operation. The equivalent using strlcpy(3) is simply:

  `

      (void)strlcpy(buf, input, sizeof(buf));

  `

### SEE ALSO

bcopy(3), memccpy(3), memcpy(3), memmove(3), strlcpy(3)

### STANDARDS

The strcpy(``) and strncpy(``) functions conform to ISO/IEC 9899:1999 (``ISO C99'') .