strcpy() Function in C

strcpy() Function in C

3 mins read231 Views Comment
Updated on Nov 23, 2023 14:51 IST

Uncover the versatility of the strcpy() function in C programming, exploring its syntax, practical applications, and code examples. Learn how strcpy() efficiently copies strings, manipulate substrings, and master string operations in C to enhance your programming prowess.

2023_03_Feature-Image-Templates-7-1p1.jpg

But, before diving deeper into the article (strcpy() function in C), let’s go through the list of topics we will cover. Here’s the table:

Table of Contents (TOC)

So, without further ado, let’s get started! 

You can also explore: Difference Between Array and Structure in C

Introduction to strcpy() Function in C 

The strcpy() function in C is used to copy a string from one location to another. It is included in the string.h header file in C. The function takes two arguments:  

  • A destination string 
  • A source string 

The source string is copied to the destination string. 

The syntax for the strcpy() function is: 

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

Parameters of strcpy() 

Here, destination is the pointer to the destination string, which is the location where the source string will be copied. source is the pointer to the source string that will be copied to the destination string.  

Note that the source parameter is declared as const char*, meaning that the source string is read-only and cannot be modified. 

This function copies the characters of the source string to the destination string until it reaches the null character , which marks the end of the string. It then adds a null character at the end of the destination string to terminate it. 

Return Value of strcpy() 

The strcpy() function returns a pointer to the destination string, which can be used for further manipulation or displayed to the user to show the copied content. 

You can also explore: Stored procedure Vs. Function: What are the differences?

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

Examples of strcpy() Function in C  

Example 1: Here’s an example code snippet that demonstrates how to copy a string into another string using strcpy() in C: 


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello, World!";
char str2[20];
// Copy str1 to str2 using strcpy()
strcpy(str2, str1);
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
return 0;
}
Copy code

Output: 

2023_03_image-73.jpg

Example 2: Copying a string to a dynamically allocated memory 


 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char* str1 = "Hello, World!";
char* str2;
// Allocate memory for str2
str2 = (char*)malloc(strlen(str1) + 1);
// Copy str1 to str2 using strcpy()
strcpy(str2, str1);
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
// Free memory allocated for str2
free(str2);
return 0;
}
Copy code

Output: 

2023_03_image-73.jpg

Example 3: Copying a substring into another string 


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello, World!";
char str2[10];
// Copy the first 5 characters of str1 to str2 using strncpy()
strncpy(str2, str1, 5);
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
return 0;
}
Copy code

Output: 

2023_03_image-76.jpg

In this third example, note how we use the strncpy() function instead of strcpy() to copy a substring of the source string to the destination string. The strncpy() function takes three arguments:  

  • The destination string 
  • The source string 
  • The maximum number of characters to copy 

Example 4: Copying a string to an uninitialized array 


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[20];
// Copy str1 to str2 using strcpy()
strcpy(str2, str1);
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
return 0;
}
Copy code

Output: 

2023_03_image-77.jpg

In this example, we declare an uninitialized character array str2 and copy the content of str1 to it using the strcpy() function. Since str2 is not initialized with any value, it will contain garbage values before the copy operation. However, after this function is called, str2 will contain the same content as str1

You can also explore: Difference between Malloc and Calloc

You must explore: Difference between strlen() and sizeof() in C

Endnotes 

In conclusion, the strcpy() function is a fundamental tool for working with strings in C and is a building block for many other string operations. By using this function correctly and in conjunction with other string manipulation functions, programmers can write powerful and efficient string-handling programs in C. 

Hope this article was helpful for you. If you want to learn more about C programming and solidify your basics, you can explore our articles on C. 

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio