In this guide, we will discuss how to use the strcpy() function in C language. The strcpy() function is a part of the C standard library and is used to perform string copy operations. It is included in the string.h header file and needs to be imported before using the function.

Basic Usage

The syntax of the strcpy function is:

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

How it works

The strcpy() function is used to copy a string that is pointed to by the source(src) pointer to the destination (dest). It takes two-pointer arguments to char or array of characters. The function copies all the characters from the source string to the destination. The function also copies the null terminating character from the source to the destination. The function returns a pointer address of the destination string.

To ensure the strcpy() function does not modify the source string, the source (src) argument is preceded with a constant modifier.

Example 1

The following simple program demonstrates how the strcpy() function works.

#include

#include

int main() {


    char src[] = “C programming”;


    char dest[100];

    printf(“Source string before copy: %s n, src);


    printf(“Destination string before copy: %s n, dest);

    strcpy(dest, src);


   


    printf(“Source string after copy: %s n, src);


    printf(“Destination string after copy: %s n, dest);

    return 0;

}

Once we run the program above, the destination string’s value should hold the source string’s value after the execution of the strcpy() function.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1-32.jpg" data-lazy- height="91" src="data:image/svg xml,” width=”606″>

Example 2

Let us take another example where the destination string already holds a value. For example: suppose we have an example program as:

#include

#include

int main() {


    char src[] = “C programming”;


    char dest[] = “Hello world from C programming”;

    printf(“Source string before copy: %s n, src);


    printf(“Destination string before copy: %s n, dest);

    strcpy(dest, src);


   


    printf(“Source string after copy: %s n, src);


    printf(“Destination string after copy: %s n, dest);

    return 0;

As shown in the above example, the destination string already contains an array of characters. Once we call the strcpy() function, its content is overwritten with the new value of the source string.

Hence, the strcpy() function does not append the content of the source string to the destination. Instead, it completely overwrites the destination string with the new value.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2-32.jpg" data-lazy- height="87" src="data:image/svg xml,” width=”582″>

Example 3

Take an example as shown below, where the size of the destination string is too small to hold the source string.

#include

#include

int main() {


    char src[] = “C programming”;


    char dest[10];

    printf(“Source string before copy: %s n, src);


    printf(“Destination string before copy: %s n, dest);

    strcpy(dest, src);


   


    printf(“Source string after copy: %s n, src);


    printf(“Destination string after copy: %s n, dest);

    return 0;

}

If you run the program above, it does not crash. However, as you can see the destination string size is way too small to hold the source string.

Since the strcpy() function does not check if the size of the destination string is enough to store the source string, it will start copying the string until it reaches the null terminating character. This will cause the program to overflow and overwrite memory locations that might be intended for other variables.

In our example above, this will cause the program to overwrite the source string itself giving an output as shown:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3-31.jpg" data-lazy- height="93" src="data:image/svg xml,” width=”480″>

Example 4

Another common mistake would be to pass a string literal to the strcpy() function as the destination string.

For example:

#include

#include

int main() {


    char src[] = “C programming”;


    printf(“Source string before copy: %s n, src);

    strcpy(” “, src);


    printf(“Source string after copy: %s n, src);


    return 0;

}

The above example program will cause an error, and the program will crash with a segmentation fault.

Closing

In this quick tutorial, we discussed various scenarios you can use the C strcpy() function to copy a string from source to destination. It is good to ensure the strcpy() function is functioning correctly to avoid bugs such as memory overflow.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/john-150×150.png612658fdeee1e.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list