How to copy a string in the C language

In C language, you can use the strcpy function to copy strings. The prototype of the strcpy function is as follows:

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

Destination refers to the target string, while source represents the source string.

Here is an example:

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

int main() {
   char source[] = "Hello, World!";
   char destination[20];

   strcpy(destination, source);

   printf("Source: %s\n", source);
   printf("Destination: %s\n", destination);

   return 0;
}

The output result is:

Source: Hello, World!
Destination: Hello, World!

Make sure that when using the strcpy function, the target string’s buffer is large enough to prevent buffer overflow.

bannerAds