Strcpy Function in C

At times, in our C programs, we might be required to copy one string to another, and in such cases, we can make use of the standard library function in C, which is the Strcpy function. Simply “Strcpy” stands for string copy, and the name says what the function is going to do.

Strcpy Function in C

In this article, we are going to have a look at a simple C program, through which, we are going to understand the Strcpy function using an example. The simple thing that we are going to do is copy one string to another.

In the given video, you can find an easy explanation along with a program for understanding the Strcpy function in C.

Here is the C program to understand the Strcpy function –

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

int main() {
char string1[] = "Hello from GyaniPandit!";
char string2[25];

// Copy the content of string1 to string2
strcpy(string2, string1);

// Printing both the strings
printf("String1: %sn", string1);
printf("String2: %sn", string2);

return 0;
}

Program Output –
String1: Hello from GyaniPandit!
String2: Hello from GyaniPandit!