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.
#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
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.
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
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
strcpyneedsstrlen(src) + 1bytes in the destination. If the source length is not known to be small, check it first or use a bounded technique.strncpydoes not guarantee termination. Ifsrchasnor more characters before its terminator, nothing terminatesdest. Always setdest[n - 1] = '\0'afterward or copy onlyn - 1bytes.strncpypads with zeros. Ifsrcis shorter thann, the rest ofdestis 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-truncationand-Wstringop-overflowfor suspicious uses. Treat these as real bugs. - Prefer
snprintf(dest, size, "%s", src). It never overflows, always terminates (ifsize > 0) and returns the length that would have been needed, so you can detect truncation. strlcpyexists 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 = bforchar *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.