C strcpy() function


strcpy() function in C is used to copy the string to another character array. This function makes changes to the original destination string and returns the pointer to the destination string.

Basic Syntax

char* strcpy(char* dest, const char* src);

And the parameters are:

Parameter Description
dest This parameter specifies the destination character array in which string should be copied
src This specifies string to be copied

Return Value

This function returns the pointer to the destination string dest.

Example

Following are the examples for strcpy() function in C.

Example 1

// C program for strcpy() function ic C and C++ 
#include 
#include 

int main () 
{ 
	char src1[]="www.CrazyGeeks.org"; 
	char src2[] = "Welcome"; 
	
	char dest1[40]; 
	char dest2[40]; 
	
	// this will copy scr1 to dest1
	strcpy(dest1, src1);
	printf ("dest1: %s\n", dest1);
	
	// this will copy scr1 to dest1
	strcpy(dest2, src2);
	printf ("dest2: %s\n", dest2);
	
	return 0; 
} 

The output should be:

dest1: www.CrazyGeeks.org
dest2: Welcome

Example 2


// C program for strcpy() function ic C and C++ 
#include 
#include 
  
int main () 
{ 
    char str1[]="www.CrazyGeeks.org"; 
    char str2[40]; 
    char str3[40]; 
      
    // this will copy str1 to str2  
    strcpy(str2, str1); 
    printf ("str2: %s", str2);
    
    // this will copy str2 to str3
    strcpy(str3, str2); 
    printf ("\nstr3: %s", str3);
    
    return 0; 
} 

The output should be:

str2: www.CrazyGeeks.org
str3: www.CrazyGeeks.org