C Standard Library

C <string.h> - strcpy() Function

The C <string.h> strcpy() function is used to copy the character string pointed to by source, including the null character, to the character array whose first element is pointed to by destination.

The behavior is undefined if the destination is not large enough for the content of source (including the null character), or if the strings overlap.


char * strcpy ( char * destination, const char * source );                   


destination Specify pointer to the character array where the content is to be copied.
source Specify pointer to the null-terminated byte string to copy from.

Return Value

Returns destination.


The example below shows the usage of strcpy() function.

#include <stdio.h>
#include <string.h>
int main (){
  char str1[50] = "xyz";
  char str2[50];
  char str3[50] = "Hello World!";

  //replacing the content of str1
  //and str2 with str3
  strcpy(str1, str3);
  strcpy(str2, str3);

  printf("str1 is: %s\n", str1);
  printf("str2 is: %s\n", str2);   
  return 0;

The output of the above code will be:

str1 is: Hello World!
str2 is: Hello World!

❮ C <string.h> Library