C programming string.h function - char *strcpy(char *dest, const char *src)

h‮‬ttps://www.theitroad.com

The strcpy function is defined in the string.h header file in C programming language. It is used to copy a null-terminated string from the source string to the destination string.

The syntax of strcpy function is:

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

Here, dest is the pointer to the destination string, and src is the pointer to the source string.

The strcpy function copies the source string to the destination string including the terminating null character. It returns the pointer to the destination string.

For example, the following code snippet copies the string "Hello, World!" from the source array src to the destination array dest using the strcpy function.

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

int main() {
    char src[] = "Hello, World!";
    char dest[50];
    
    // Copy the string from src to dest
    strcpy(dest, src);
    
    printf("Source string: %s\n", src);
    printf("Destination string: %s\n", dest);
    return 0;
}

Output:

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