C strcpy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, strcpy() copies a whole C-string into another array, including the ending '\0', so the destination holds the same text.

You pass the destination and the source. Both are required. The destination must already be large enough for every character plus '\0'. Include <string.h>.

Syntax

Pass the destination, then the source. Start with this call.

C
strcpy(dest, "Hello");
CallWhat it does
strcpy(dest, "Hello")Copies Hello and the ending '\0' into dest.

Include <string.h>. The call does not grow the destination array.

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

Parameters

ParameterDescription
destRequired. The array you copy into. It must already be large enough.
srcRequired. The C-string you copy from. It is not changed.

Return Value

Return ValueDescription
dest pointerThe same destination pointer you passed in.

C Examples

Example 1 — Basic String Copy

Copy a literal into a char array large enough for the text and '\0'.

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

int main(void) {
    char dest[16];
    const char *src = "Hello";

    strcpy(dest, src);
    printf("dest = %s\n", dest);

    return 0;
}

How It Works

  1. Prepare dest. dest[16] has room for “Hello” (5 chars) plus '\0'.
  2. Call strcpy. Characters and the terminator move from src into dest.
  3. Print as a string. Because '\0' was copied, %s works.
  4. No length argument. Copying stops at the source terminator automatically.

Example 2 — Copy and Check Length

After copying, use strlen to confirm the destination length matches the source.

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

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

    strcpy(dest, src);
    printf("dest   = %s\n", dest);
    printf("length = %zu\n", strlen(dest));

    return 0;
}

How It Works

  1. Copy the text. Nine letters plus '\0' land in dest.
  2. Measure with strlen. strlen counts characters before '\0' → 9.
  3. Same content. Printing shows dest matches src.
  4. Room to spare. dest[32] is larger than needed, so the copy is safe.

Example 3 — Overwrite the Destination

A second strcpy replaces the previous contents. Old characters beyond the new terminator are ignored by %s.

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

int main(void) {
    char dest[16];

    strcpy(dest, "Apple");
    printf("first  = %s\n", dest);

    strcpy(dest, "Pie");
    printf("second = %s\n", dest);

    return 0;
}

How It Works

  1. First copy. dest becomes "Apple".
  2. Second copy. "Pie" and its '\0' overwrite the start of the buffer.
  3. %s stops early. Printing stops at the new terminator, so you see Pie.
  4. Not append. To join strings, use strcat, not a second strcpy.

Frequently Asked Questions

strcpy() copies the whole source C-string into dest, including the ending '\0'.
Both are required. dest is the array you copy into, and src is the text you copy from.
The same dest pointer you passed in.
Include <string.h>.
No. You must make sure dest is large enough.

Next: strncpy()

Copy at most n bytes—and learn when the terminator is still missing.

strncpy() tutorial →

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.

9 people found this page helpful