The strcpy() function copies a C-string from src into a writable buffer dest, including the terminating '\0'. It is the classic way to duplicate string content into your own memory so you can modify it independently.
01
Copy
src → dest.
02
Includes \0
Null terminated.
03
Returns dest
Same pointer.
04
Needs space
strlen + 1.
05
string.h
Standard C.
06
vs strncpy
Bounded copy.
Fundamentals
Definition and Usage
strcpy stands for string copy. It reads bytes from src one at a time and writes them to dest until it copies the null terminator. After the call, dest holds a complete C-string equal to src.
The destination must be modifiable memory—a char array or malloc buffer—not a string literal. You must allocate at least strlen(src) + 1 bytes because strcpy performs no bounds checking.
💡
Beginner Tip
Before strcpy(dest, src), verify: “Is dest at least strlen(src) + 1 bytes?” When sizes are unknown, prefer snprintf(dest, size, "%s", src) or allocate with strdup (POSIX).
Foundation
📝 Syntax
Standard C declaration:
C
char *strcpy(char *dest, const char *src);
Parameters
dest — writable buffer that will receive the copy.
strcpy copies 14 characters including the final '\0'. The 20-byte destination array has plenty of room. Source and destination are separate arrays, so changing one does not affect the other.
Example 2 — Modify the Copy Independently
After copying, change destination without touching source.
original points to read-only string data. Copying into working gives you a modifiable buffer. Replacing index 4 changes the copy only.
📈 Practical Patterns
Size checks, return value, and safer alternatives.
Example 3 — Check Buffer Size First
Verify there is enough room before calling strcpy.
C
#include <stdio.h>
#include <string.h>
int main(void) {
const char *src = "Learning C strings";
char dest[32];
size_t need = strlen(src) + 1;
size_t cap = sizeof dest;
if (need > cap) {
printf("Buffer too small: need %zu, have %zu\n", need, cap);
return 1;
}
strcpy(dest, src);
printf("Copied: %s\n", dest);
return 0;
}
📤 Output:
Copied: Learning C strings
How It Works
strlen(src) + 1 counts characters plus the null byte. Comparing to sizeof dest prevents overflow when the source fits. If it does not fit, choose a larger buffer or truncate with a bounded function.
Example 4 — Using the Return Value
strcpy returns dest, so you can chain or pass it directly.
The return value equals buf. This pattern appears in older code; modern style usually calls strcpy on its own line for clarity.
Example 5 — Safer Copy with snprintf()
When you know the buffer size, snprintf avoids writing past the end.
C
#include <stdio.h>
#include <string.h>
int main(void) {
const char *src = "A long message that must fit safely";
char dest[20];
if (snprintf(dest, sizeof dest, "%s", src) >= (int)sizeof dest) {
printf("Warning: string truncated to fit buffer.\n");
}
printf("Result: %s\n", dest);
return 0;
}
📤 Output:
Warning: string truncated to fit buffer.
Result: A long message th
How It Works
snprintf writes at most sizeof dest - 1 characters and always null-terminates. This is safer than strcpy when the source length might exceed the buffer. For exact copies with known sizes, strcpy remains fine.
Applications
🚀 Common Use Cases
Duplicate input — copy user text into a local buffer for editing.
Default values — initialize a char array from a string literal template.
Parse pipelines — copy a token before passing it to functions that modify strings.
Struct fields — fill fixed-size char name[N] members from constants.
Learning foundation — understand before moving to strncpy and dynamic allocation.
🧠 How strcpy() Works
1
Read byte from src
Start at src[0] and advance.
Read
2
Write to dest
Copy each byte to the next dest position.
Write
3
Stop at \0
Copy the null terminator and finish.
Terminate
=
📄
char* dest
dest now holds a duplicate C-string of src.
Important
📝 Notes
Destination must be writable—never strcpy into a string literal.
Allocate at least strlen(src) + 1 bytes in dest.
dest and src must not overlap—use memmove instead.
No length check—overflow is a common security bug.
Modern code often prefers snprintf, strncpy (with care), or strdup.
Performance
⚡ Optimization
Library strcpy implementations are tuned for speed on typical strings. Avoid calling strlen before every strcpy unless you need the length for a bounds check—one pass for copy alone is enough when the buffer is known to fit.
Wrap Up
Conclusion
strcpy() copies a full C-string into a buffer you provide. Size dest correctly, avoid overlap, and consider bounded alternatives when input length is unpredictable.
Next, learn strncpy() for copies with a maximum byte limit.
strcpy() copies the source C-string src into the destination buffer dest, including the null terminator '\0'. The result is stored in dest. It does not allocate new memory.
char* strcpy(char* dest, const char* src); Include <string.h>. dest must be a modifiable char array or heap buffer with enough space for strlen(src) + 1 bytes.
It returns dest—the same pointer passed as the first argument. Chaining is possible but uncommon; most code uses dest directly after the call.
No. strcpy() has no length parameter. If dest is too small, bytes are written past the end of the buffer—buffer overflow. Always ensure dest has room for the full source string plus '\0'.
strcpy() always copies until the source '\0'. strncpy(dest, src, n) copies at most n bytes and may leave dest without a null terminator if n is too small. strncpy can limit writes but must be used carefully.
No. If dest and src overlap, behavior is undefined. Use memmove() for overlapping memory copies, not strcpy().
Did you know?
C23 introduces strcpy_s in optional bounds-checking interfaces, and many codebases ban raw strcpy in favor of snprintf or platform-specific safe functions. Learning strcpy still matters because it explains how C-strings are copied byte by byte.