Difference Between strcpy() and strcat() Function in C
String manipulation is an essential concept in C programming. The C standard library provides functions like strcpy() and strcat() to copy and concatenate strings efficiently. In this post, we’ll examine how strcpy() and strcat() are used as well as some of their main distinctions.
The strcpy() Function in C
The strcpy() function inserts the whole string (including the null terminator) that is referenced by the source into the destination array.
The syntax is:
char *strcpy(char *destination, const char *source);
Let’s look at an example:
#include <stdio.h> #include <string.h> int main() { char source[20] = "Hello World"; char destination[20]; strcpy(destination, source); printf("Source string: %s\n", source); printf("Destination string: %s\n", destination); return 0; }
Output:
Source string: Hello World
Destination string: Hello World
Here, the source string “Hello World” is copied into the destination array using strcpy().
Some key points about strcpy():
- Overwrites destination string fully even if arrays overlap
- Faster than strcat() in most cases
- Risk of buffer overflow if destination is too small
The strcat() Function in C
The strcat() function concatenates two strings by appending a copy of source to the end of destination.
The syntax is:
char *strcat(char *destination, const char *source);
Let’s look at an example:
#include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello"; char str2[20] = "World"; strcat(str1, str2); printf("str1 before concat: %s\n", str1); printf("str2 string: %s\n", str2); printf("Concatenated string: %s\n", str1); return 0; }
Output:
str1 before concat: Hello
str2 string: World
Concatenated string: HelloWorld
Here, strcat() appends str2 to str1, resulting in the concatenated string.
Key properties of strcat():
- The destination must have a null terminator before concatenating
- Original destination value is not overwritten, only appended to
- Risk of buffer overflow if destination buffer is too small
Common Example
#include <stdio.h> #include <string.h> int main() { char name1[10] = "DataFlair"; char name2[10] = "Rohan"; // Changed from "John" to "Rohan" char name3[10]; strcpy(name3, name1); printf("%s\n", name3); // Output: DataFlair strcat(name1, name2); printf("%s\n", name1); // Output: DataFlairRohan return 0; }
Explanation:
- Two character arrays, name1 and name2, are declared and initialized with “DataFlair” and “Rohan,” respectively.
- An empty character array name3 is declared.
- The strcpy function copies name1 to name3, resulting in name3 containing “DataFlair.”
- The printf function displays the content of name3 (“DataFlair”).
- The strcat function concatenates name2 to the end of name1, making name1 “DataFlairRohan.”
- The printf function displays the content of name1 (“DataFlairRohan”).
- The program exits with a status code of 0.
#include <stdio.h> #include <string.h> int main() { char source[100] = "This is a source string"; char dest[100]; // Copy source to dest using strcpy() strcpy(dest, source); // Concatenate additional string using strcat() strcat(dest, " - Extra appended text"); printf("Source string: %s\n", source); printf("Destination string: %s\n", dest); return 0; }
Output:
Source string: This is a source string
Destination string: This is a source string – Extra appended text
In this program:
- strcpy() is used to copy the source string into destination string.
- strcat() is then used to append additional text to the destination string.
- Finally, both source and destination strings are printed to demonstrate the result.
When to Use Each Function
Based on their behavior, here are some guidelines on when to use each one:
- Use strcpy() when you need to completely replace the destination string
- Use strcat() when you need to append two strings by concatenating
- Use strcpy() in time-critical sections for better performance
- Use strcat() when maintaining null terminated substrings is required
Key Differences Between C strcpy() and strcat()
Feature | strcpy() | strcat() |
Behavior | Copies source string to destination | Appends source string to destination |
Overwriting | Overwrites destination fully | Does not overwrite, only appends |
Null-termination | Not required in the destination | Required in destination before concatenating |
Return value | Returns destination string | Returns concatenated string |
Performance | Faster | Slower |
Use cases | Complete replacement of destination needed | Appending strings by concatenating |
Buffer issues | Can cause overflow if destination is too small | Can cause overflow if destination is too small after appending |
Conclusion
In summary, strcpy() and strcat() allow efficient string manipulation in C but have some key differences. strcpy() overwrites the destination while strcat() appends to it. Keeping their exact behavior in mind helps us pick the right function and use it safely by managing null termination and buffers.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google