Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

strcpy — copying a string

strcpy copies a string including its terminating '\0'. The caller must provide enough destination space and ensure objects do not overlap.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <string.h>
char *strcpy(char * restrict s1, const char * restrict s2);

strcpy, declared in <string.h>, copies the string from s2 to s1, including the terminating '\0'. It returns s1, not the number of characters copied.

The source must contain a valid null-terminated string. The destination must be writable and hold at least strlen(s2) + 1 bytes. The function takes no buffer size and allocates no memory. An undersized buffer or overlapping copied objects causes undefined behavior; do not expect an error code.

In the example, sizeof source includes the null terminator, so destination is large enough. The separate arrays do not overlap. puts prints the copy and adds a newline.

Examples

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

int main(void)
{
    const char source[] = "Hello";
    char destination[sizeof source];

    strcpy(destination, source);
    puts(destination);
    return 0;
}

Result

Hello

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources