Skip to content
Tutorials Center
My learning

Topics

C Reference ยท string.h

C strcpy() and strncpy() functions

Reference for C strcpy() and strncpy(): copy a string, why strcpy overflows, why strncpy may leave no terminator, and safer snprintf-based patterns.

strcpy() copies a null-terminated string, including its terminator, into a destination buffer. strncpy() copies at most n bytes. Both return the destination pointer, and neither checks that the destination is large enough.

That missing check makes strcpy a classic source of buffer overflows. strncpy looks like the safe version, but it was designed for fixed-width fields, not for ordinary strings: it can leave the result without a terminator, and it pads with zeros. Most modern code uses snprintf or memcpy with an explicit length instead.

C
#include <string.h>

char *strcpy(char *restrict dest, const char *restrict src);
char *strncpy(char *restrict dest, const char *restrict src, size_t n);

Try it

C strcpy and strncpy basics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Safer copy helpers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
char *strcpy(char *restrict dest, const char *restrict src);
char *strncpy(char *restrict dest, const char *restrict src, size_t n);
Name Description
dest Destination buffer. Must be large enough for the copy.
src Source string. Must be null-terminated (for strncpy, terminated or at least n readable bytes).
n (strncpy) The number of bytes to write into dest.

Returns: dest.

Errors / undefined behavior: if dest is too small, the copy writes past the end of the buffer (undefined behavior). The two buffers must not overlap, because of restrict. For overlapping data use memmove.

Usage notes

  • strcpy needs strlen(src) + 1 bytes in the destination. If the source length is not known to be small, check it first or use a bounded technique.
  • strncpy does not guarantee termination. If src has n or more characters before its terminator, nothing terminates dest. Always set dest[n - 1] = '\0' afterward or copy only n - 1 bytes.
  • strncpy pads with zeros. If src is shorter than n, the rest of dest is filled with '\0'. Copying a short name into a large buffer therefore writes the whole buffer, which is wasteful. This padding is the behavior it was designed for: fixed-size, zero-padded record fields.
  • Compilers warn. GCC reports -Wstringop-truncation and -Wstringop-overflow for suspicious uses. Treat these as real bugs.
  • Prefer snprintf(dest, size, "%s", src). It never overflows, always terminates (if size > 0) and returns the length that would have been needed, so you can detect truncation.
  • strlcpy exists on BSD and recent glibc versions, and is expected in a future C standard, but it is not available everywhere. See safe string handling.
  • Assignment does not copy strings. a = b for char * copies a pointer, and arrays cannot be assigned at all. Use a copy function.
  • Copying into a string literal is undefined behavior. A literal such as "abc" must not be modified; copy into your own array.
  • Duplicate with allocation using strdup.