For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
In C programming, strcpy in C is a standard library function used to copy one string into another. It simplifies string manipulation by allowing direct character transfer from a source string to a destination string. This function is widely used when handling character arrays, especially in applications that require low-level memory control.
Explore Online Software Engineering Courses from top universities.
In this article, we will explore what strcpy() does, how it works, and when to use it. You will learn its syntax, understand its return value, and see practical examples at various difficulty levels. We will also discuss its advantages, limitations, and important safety considerations.
In C programming, strcpy() is a standard library function used to copy the contents of one string (source) into another (destination). It is defined in the <string.h> header file. This function plays a key role in string manipulation, especially when working with character arrays.
The strcpy() function copies each character from the source string to the destination string until it encounters the null terminator ('\0'). Once copying is complete, the destination string becomes an exact copy of the source.
Let’s understand this with a simple example.
#include <stdio.h>
#include <string.h> // Required for strcpy()
int main() {
char source[] = "Hello, World!"; // Source string to copy
char destination[50]; // Destination string with enough space
strcpy(destination, source); // Copying source into destination
// Display the copied string
printf("Copied String: %s\n", destination);
return 0;
}
Output:
Copied String: Hello, World!
Explanation:
The strcpy() function is fast and efficient, but it does not check for buffer overflow. So, when using strcpy in C, make sure the destination array is large enough to hold the source string and the null terminator.
The strcpy() function in C is defined in the <string.h> header file. It copies the contents of one string into another. This function works at the predefined library level and uses two parameters: a destination and a source.
Here’s the syntax of strcpy() function in C:
char *strcpy(char *dest, const char *src);
Explanation of Components:
The destination should have enough memory allocated to store the entire source string, including the null terminator.
To help you better understand the syntax, here’s an example:
#include <stdio.h>
#include <string.h> // Required for strcpy()
int main() {
char src[] = "C programming"; // Source string
char dest[30]; // Destination with enough space
// Copy src to dest using strcpy()
char *result = strcpy(dest, src);
// Print the result
printf("Destination: %s\n", dest);
printf("Returned Pointer: %s\n", result);
return 0;
}
Output:
Destination: C programming
Returned Pointer: C programming
Explanation:
In C, using strcpy() is straightforward. Just remember that the destination buffer must be large enough. If not, it may lead to undefined behavior.
Must Explore: Static Function in C: Definition, Examples & Real-World Applications
Let’s look at how strcpy() in C works through practical examples. These will help you understand its behavior at different levels of complexity.
Here’s a basic level example:
#include <stdio.h>
#include <string.h> // Required for strcpy()
int main() {
char source[] = "Welcome to C";
char destination[20]; // Ensure enough space
strcpy(destination, source); // Copying source into destination
printf("Copied String: %s\n", destination);
return 0;
}
Output:
Copied String: Welcome to C
Explanation:
Also read the strlen() Function in C article!
Here’s an intermediate level example:
#include <stdio.h>
#include <string.h>
int main() {
char names[3][30] = {"Ram", "Shubham", "Harsh"};
char copy[30];
for (int i = 0; i < 3; i++) {
strcpy(copy, names[i]); // Copy each name one by one
printf("Name %d Copied: %s\n", i + 1, copy);
}
return 0;
}
Output:
Name 1 Copied: Ram
Name 2 Copied: Shubham
Name 3 Copied: Harsh
Explanation:
Must Explore: Enumeration (or enum) in C article!
Here’s an advanced level example:
#include <stdio.h>
#include <string.h>
int main() {
char input[100];
char message[100];
printf("Enter a message: ");
fgets(input, sizeof(input), stdin); // Takes user input with spaces
// Remove newline character if present
input[strcspn(input, "\n")] = '\0';
strcpy(message, input); // Copying user input
printf("Message Stored: %s\n", message);
return 0;
}
Output:
Enter a message: Learn C programming!
Message Stored: Learn C programming!
Explanation:
These examples show how strcpy in C can be used in different real-life scenarios - from basic operations to input handling.
Also read the C Function Call Stack article!
The strcpy() function in C offers several benefits when it comes to handling strings. It’s one of the most commonly used functions in the C string handling library. Below are the key advantages of using strcpy() in C programming:
Must Explore: Library Function in C article!
While strcpy() in C is useful, it also comes with serious limitations. If not used carefully, it can lead to program crashes, undefined behavior, or security risks. Below are the major drawbacks every C programmer must be aware of.
Example: Unsafe Use of strcpy() in C
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "This string is too long!";
char destination[10]; // Not enough space
strcpy(destination, source); // Unsafe operation
printf("Copied String: %s\n", destination);
return 0;
}
Output:
Copied String: This string is too long!
Explanation:
Safe Alternative (Recommended)
Use strncpy() or strcpy_s() if you want safer copying with size limits.
strncpy(destination, source, sizeof(destination) - 1);
destination[sizeof(destination) - 1] = '\0'; // Null-terminate manually
This ensures safety while copying strings in C.
The strcpy in C function is a simple yet powerful tool to copy strings from one variable to another. It belongs to the standard C string library and is widely used in real-world applications. However, it should be handled with care to avoid unsafe behavior.
Use strcpy() in C when you know the size of your arrays and the input is predictable. For safer alternatives, especially when handling user input, prefer strncpy() or strcpy_s(). Mastering string handling is essential in C, and strcpy() is the first step. Use it wisely to write cleaner, faster, and more reliable programs.
The strcpy() function in C copies a string from a source to a destination. It replaces the contents of the destination string with those of the source, including the null terminator '\0'. It does not perform boundary checks.
To use strcpy() in your C program, you must include the <string.h> header file. This file contains standard string handling functions, including strcpy(), strlen(), strcmp(), and many others used for manipulating C-style strings.
If the destination array is smaller than the source string, strcpy() causes a buffer overflow. This can lead to memory corruption, unpredictable behavior, or even program crashes. Always make sure the destination has enough space.
Yes. The strcpy() function returns a pointer to the destination string. This is useful for chaining operations or assigning the copied string to another variable. However, the return value is not always needed in basic use cases.
Yes, you can copy strings manually using a loop. But strcpy() simplifies the task and reduces code size. However, manual copying gives you better control over bounds checking and avoids the risks that come with strcpy().
For a better understanding, here’s the difference in a tabular format:
Feature | strcpy() | strncpy() |
Null Terminator | Automatically added | May not be added if limit is reached |
Buffer Safety | No size check | Allows specifying max characters to copy |
Speed | Generally faster | Slightly slower due to extra checks |
Ideal Use Case | Controlled, fixed-size strings | User input, dynamic or unknown input size |
Yes, strcpy() can copy an empty string. When the source string is "", only the null terminator '\0' is copied to the destination. It doesn’t result in an error as long as the destination is valid.
No, it is not safe for copying direct user input. If the input exceeds the destination buffer size, it can crash the program. For such cases, prefer strncpy() or safer variants like strcpy_s() if supported.
Yes, strcpy() works with dynamically allocated memory. Just ensure the destination pointer is valid and points to a memory block large enough to hold the entire source string, including the null character.
No, strcpy() is not designed to handle overlapping memory regions. Using it in such cases results in undefined behavior. If overlap is possible, use memmove() instead, as it safely handles overlapping memory blocks.
To avoid errors, always allocate enough memory for the destination. Use safer alternatives like strncpy() or strcpy_s() if available. Always validate input sizes, especially when copying user-controlled or variable-length strings.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.