C strcpy()

The strcpy() function is defined in the string.h header file. It helps to copy the string pointed by the source into the string pointed by destination.


char *strcpy(char *dest, const char *src); #where dest and src should be strings

 

strcpy() Parameters: 

The strcpy() function takes two parameters. This function copies the string pointed by the 'src' including the null character.

Parameter Description Required / Optional
dest Pointer to the destination array where the content is to be copied Required
src the string to be copied Required

strcpy() Return Value

The function returns a pointer to the resulting destination string. If the size of the destination string is not enough to store the copied string it may result in undefined behavior.

Input Return Value
if parameters pointer to destination string

Examples of strcpy() 

Example 1: Working of strcpy() function in C?


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

int main()
{
   char src[30];
   char des[90];
  
   memset(des, '\0', sizeof(des));
   strcpy(src, "This is learnetutorials.com");
   strcpy(des, src);

   printf("After copying string is : %s\n", des);
   
   return(0);

}

Output:


After copying string is :This is learnetutorials.com

Example 2: How strcpy() works in C?


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

int main (){

  char string1[30] = "learnetutorials.com";
  char string2[30];

  // copying string1 to string2
  strcpy(string2, string1);

  puts(string2); 

  return 0;
}

Output:


learnetutorials.com