C String strcpy() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<string.h>

What You’ll Learn

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.

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).

📝 Syntax

Standard C declaration:

C
char *strcpy(char *dest, const char *src);

Parameters

  • dest — writable buffer that will receive the copy.
  • src — null-terminated source string (not modified).

Return Value

  • Returns dest (pointer to the destination buffer).
  • The copied string lives in dest; no new string object is created.

Header

  • #include <string.h>

⚡ Quick Reference

srcdest size neededdest after strcpy
"Hello, World!"14 bytes minimum"Hello, World!"
"" (empty)1 byte""
"C"2 bytes"C"
required bytesstrlen(src) + 1 (includes '\0')
Copy
strcpy(dest, src);

Full string

Size
strlen(src) + 1

Min dest

Safe alt
snprintf(d, n, "%s", s)

Bounded

Overlap
memmove(dest, src, n)

Not strcpy

Examples Gallery

Compile with gcc strcpy.c -std=c11 -o strcpy. Every example uses a destination buffer large enough for the source string.

📚 Getting Started

Copy a string from source to destination and print both.

Example 1 — Basic String Copy

Copy "Hello, World!" into a char array.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char source[] = "Hello, World!";
    char destination[20];

    strcpy(destination, source);

    printf("Source:      %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}

How It Works

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.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *original = "CodeToFun";
    char working[32];

    strcpy(working, original);
    working[4] = '-';

    printf("Original: %s\n", original);
    printf("Working:  %s\n", working);

    return 0;
}

How It Works

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;
}

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.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[24];

    printf("Copied: %s\n", strcpy(buf, "strcpy returns dest"));

    return 0;
}

How It Works

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;
}

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.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Ensure dest has strlen(src) + 1 bytes
  • Use a char array or malloc buffer for dest
  • Check size before copy when lengths vary
  • Prefer snprintf when truncation is acceptable
  • Use memmove for overlapping memory

❌ Don’t

  • Copy into string literals
  • Assume any buffer is large enough
  • Let dest and src overlap
  • Use strcpy on untrusted input without bounds
  • Forget that strcpy always writes a trailing '\0'

Key Takeaways

Knowledge Unlocked

Five things to remember about strcpy()

Use these points when copying strings in C.

5
Core concepts
📝 02

Writable dest

Not literal.

Storage
🔢 03

Size = len+1

No bounds.

Safety
📈 04

Returns dest

Same ptr.

Return
💬 05

vs strncpy

Limit n.

Compare

❓ Frequently Asked Questions

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.

Continue the String Functions Series

Master full string copies with strcpy(), then learn bounded copies with strncpy().

Next: strncpy() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful