strcpy is a function in the C language standard library that is used to copy a string from a source location to a destination location.

strcpy Function Header File

strcpy function header file is <string.h>. Before using the strcpy function, make sure to include this header file in your C code:

#include <string.h>

strcpy Function Prototype

char* strcpy(char* restrict s1,
	const char* restrict s2);

The strcpy function copies the string pointed to by s2 (including the terminating character '\0') into the array pointed to by s1. If copying is done between overlapping objects, the behavior is undefined.

Parameter Description

  • s1: Pointer to the target string buffer, where the copied string will be stored.
  • s2: Pointer to the source string, specifying the string content to be copied.

Return value

strcpy function returns the pointer of the target string, that is, s1.

strcpy Example Code

This is a simple example of the strcpy function, which demonstrates how to copy a string from a source location to a target location.

It should be noted that the target buffer must be large enough to accommodate the source string, otherwise it may cause a buffer overflow problem.

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

int main() {
    char source[] = "Hello, world!";
    char destination[20]; // Make sure the destination buffer is large enough

    strcpy(destination, source);

    printf("%s\n", destination);

    return 0;
}

Program Running Effect

Hello, world!