Open In App

strcpy() in C

Last Updated : 24 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, strcpy() is a built-in function used to copy one string into another. It is a part of the C standard strings library, which provides various functions to manipulate strings efficiently.

Let's take a look at an example:

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

int main() {
    char s1[] = "Hello, Geeks!";
  
    // Ensure destination has enough space
    char s2[50];  

    // Copying string using strcpy
    strcpy(s2, s1);

    printf("%s", s2);
    return 0;
}

Output
Hello, Geeks!

Explanation: The function takes two arguments: a destination buffer where the copied string will be stored, and a source string that will be copied. Here, s1 is source string and s2 is a destination string. The function copies the entire source string, including the null terminator, into the destination buffer. It does not perform bounds checking, meaning it will overwrite memory if the destination buffer is not large enough to hold the copied string.

Syntax of strcpy()

strcpy(dest, src);

Parameters

  • dest: Pointer to the destination character array where the content is to be copied.
  • src: Pointer to the source character array which is to be copied.

Return Value

  • This function returns a pointer to dest string.

Examples of strcpy()

The below examples demonstrate the working and use of strcpy() functions:

Copy String Literal

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

int main() {
  
    char s1[] = "Hello Geeks!";
    char s2[20] = "GfG";
    char s3[40];

    // Copy contents of s1 to s2 using strcpy
    strcpy(s2, s1);

    // Copy a constant string into s3
    strcpy(s3, "Copy successful");

    printf("s1: %s\n", s1);
  	printf("s2: %s\n", s2);
  	printf("s3: %s", s3);
  	
    return 0;
}

Output
str1: Hello Geeks!
str2: Hello Geeks!
str3: Copy successful

Explanation: This program demonstrates the use of the strcpy() function to copy strings in C. It copies the contents of s1 to s2, a predefined message to s3. The final result is printed to show the contents of all strings. The strcpy() function is used to copy one string into another, and the program prints the updated values of each string.

Risk of Buffer Overflow (Unsafe Use)

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

int main() {
    char s1[] = "Hello! Welcome to GeeksforGeeks";
  
    // Small buffer
    char s2[5];
    
    // Some other array
    char name[10] = "Rahul";

    // Copying string using strcpy (unsafe)
    // Unsafe: will cause buffer overflow
    // which may overwrite the name
    strcpy(s2, s1);

    printf("Dest: %s\n", s2);
    printf("Name: %s\n", name);

    return 0;
}

Output
Dest: Hello! Welcome to GeeksforGeeks
Name: o GeeksforGeeks

Explanation: The size of the buffer s2 in which we were copying the string s1, the strcpy() still copies the whole s1 even after the memory allocated ends only at 5 bytes. This overwrites the other data of the next declared string name.

Safe Alternative: strncpy

To avoid buffer overflow, consider using strncpy, which allows you to specify the maximum number of characters to copy, thus preventing overflow.

strncpy(dest, stc, sizeof(dest) - 1);
dest[sizeof(dest) - 1] = '\0';

Explanation:

  • strcpy is used to copy a string from one location to another in C.
  • It is simple but lacks bounds checking, which can lead to buffer overflow.
  • Always ensure the destination array has enough space or consider using strncpy for safer string copying.

Next Article
Article Tags :

Similar Reads