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
The strcat() function in C is used to join two strings. It adds one string to the end of another. This function is part of the C standard library and comes from the <string.h> header file. It is simple to use but must be handled carefully.
Explore Online Software Engineering Courses from top universities.
In this article, you will learn how the strcat() function works, how to use it, and what mistakes to avoid. We will also cover the function’s syntax with easy-to-follow examples. These examples will range from basic to advanced levels. Finally, our FAQ section answers real questions that C programmers often ask about string concatenation and strcat().
The strcat() function in C is a built-in string handling function. It joins one string to the end of another. This operation is known as string concatenation. The function is defined in the standard library <string.h> and is commonly used in C programming for combining two strings.
When you use strcat(), it takes two arguments: a destination string and a source string. It adds the source string at the end of the destination string. This means the null character ('\0') at the end of the destination string is replaced. Then, the characters from the source string are copied, followed by a new null character.
The destination string must have enough space to store the final combined result. If not, it may lead to undefined behavior like buffer overflow.
Must Explore: Static Function in C: Definition, Examples & Real-World Applications
Here’s the syntax of strcat() in C:
char *strcat(char *dest, const char *src);
The function accepts the following parameters:
Return Value:
The function returns a pointer to the final string dest, which now contains the concatenated result.
Also read the strlen() Function in C article!
In this section, we’ll explore how the strcat() function in C works through practical examples. The examples are divided into three levels - basic, intermediate, and advanced.
Let’s begin with a simple example. This shows how to concatenate two strings using strcat() in a safe and readable way.
#include <stdio.h>
#include <string.h> // Required for strcat()
int main() {
char str1[30] = "Good "; // Destination string with enough space
char str2[] = "Morning"; // Source string to be appended
strcat(str1, str2); // Appends str2 to str1
printf("Final String: %s\n", str1); // Output the result
return 0;
}
Output:
Final String: Good Morning
Explanation:
Now let’s see how to combine multiple strings step by step. This example joins three separate strings into one.
#include <stdio.h>
#include <string.h>
int main() {
char message[100] = "Welcome "; // First part
char name[] = "to "; // Second part
char place[] = "C Programming!"; // Third part
strcat(message, name); // Add "to " to "Welcome "
strcat(message, place); // Add "C Programming!" to the result
printf("Combined Message: %s\n", message);
return 0;
}
Output:
Combined Message: Welcome to C Programming!
Explanation:
In this advanced case, we use strcat() with user input. It shows how to safely combine strings entered by the user at runtime.
#include <stdio.h>
#include <string.h>
int main() {
char greeting[100] = "Hello, "; // Predefined greeting
char username[50]; // To store user input
printf("Enter your name: ");
fgets(username, sizeof(username), stdin); // Read input including spaces
// Remove trailing newline if present
username[strcspn(username, "\n")] = 0;
strcat(greeting, username); // Append user's name to the greeting
printf("Personalized Greeting: %s\n", greeting);
return 0;
}
Sample Output:
Enter your name: Tarun
Personalized Greeting: Hello, Tarun
Explanation:
Must Explore: Enumeration (or enum) in C article!
You should use the strcat() function in C when you are preparing dynamic messages or handling string-based output. Below are key scenarios where strcat() is the right choice:
Also Read: Strcpy in C: How to Copy Strings with Examples article!
While the strcat() function is useful, it’s important to be cautious of certain pitfalls when using it. Below are the most common errors that developers encounter when working with strcat(), and how to avoid them:
Example: Common Error - Buffer Overflow
Here's an example of an error caused by buffer overflow:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "Hello"; // Destination array too small
char str2[] = "World!"; // Source string
strcat(str1, str2); // This may cause a buffer overflow
printf("%s\n", str1); // Output
return 0;
}
Output (likely undefined):
Error or corrupted output, may crash the program
Explanation:
How to fix it? Ensure the destination string has enough space, like this:
char str1[50] = "Hello";
In this article, we explored the strcat() function in C, which is a powerful tool for concatenating two strings. We have covered the syntax, practical examples, and common errors you might face while using it. To summarize:
We also provided examples at different levels of complexity, from basic to advanced, to help you understand how strcat() works in various situations. By now, you should be comfortable using this function safely and efficiently in your C programs.
If you follow these practices, you can avoid most common issues and use the strcat() function effectively.
The strcat() function in C appends one string to another. It takes two strings: a destination string and a source string. The source string is added to the end of the destination string. The function automatically adds a null terminator to mark the end of the string.
The syntax for using the strcat() function in C is as follows:
char *strcat(char *dest, const char *src);
Here, dest is the destination string that will hold the final concatenated result, and src is the string to be appended. The function returns a pointer to the destination string.
The strcat() function does not handle memory allocation. You must ensure that the destination string has enough space to hold the concatenated result. If there is insufficient space, strcat() may cause a buffer overflow, leading to undefined behavior.
Yes, the strcat() function in C can concatenate strings at runtime. You can append strings based on user input or program logic. Just ensure the destination string has enough memory space to hold the final result, including the null terminator.
If the destination string is uninitialized, using strcat() will likely cause a segmentation fault or undefined behavior. Always initialize the destination string with a valid value and ensure it points to enough memory before using strcat().
No, using strcat() with overlapping strings can lead to unpredictable behavior. The source and destination strings should not share memory locations. If they do, data corruption may occur. It’s important to ensure that the source and destination strings do not overlap.
The null terminator (\0) is essential when using the strcat() function. It marks the end of a string. strcat() appends the source string to the destination string, and the null terminator is automatically added at the end of the concatenated result.
Yes, you can concatenate multiple strings with strcat(). You would need to call strcat() multiple times. Each time, it will append the next string to the growing destination string. Ensure the destination string has enough space to hold the final result.
Some common errors with strcat() include buffer overflow, uninitialized destination strings, and using overlapping strings. These errors can lead to crashes or data corruption. Always check that the destination string has enough space and is properly initialized.
To prevent buffer overflow when using the strcat() function, ensure that the destination string has enough space to accommodate the concatenated result. Use sizeof() to check available memory and make sure there’s enough room for both strings and the null terminator.
Initializing the destination string is crucial because it ensures that the strcat() function appends the source string to a valid memory location. If the destination string is uninitialized or NULL, it will cause undefined behavior or crashes during program execution.
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.