Concatenate Strings with strcat() Function in C

Concatenate Strings with strcat() Function in C

3 mins read1.3K Views 1 Comment
Updated on Jul 10, 2024 16:18 IST

This article offers a comprehensive explanation of the syntax and implementation of the strcat() function, in addition to providing several examples to aid in understanding its operation. By the end of this blog, you will know how to concatenate strings with this function. 

2023_03_Strcat-function-in-C p1.jpg

If you have recently read up on C programming language, you must learn about the strcat() function in C. It is used to append or concatenate C strings. We will be elaborating on the following.

Introduction to strcat() Function in C

The strcat() function in C concatenates two strings. That means it appends the characters of the second string at the end of the first string. It shows in the string.h header file.

The syntax of strcat() function is:


 
char *strcat(char *destination, const char *source)
Copy code

Parameters of strcat()

As you can see, the strcat() function takes two arguments:

  • destination – the pointer to the destination string, which is the location where the source string will be appended.
  • source – the pointer to the source string that will be appended to the destination string. 

Do note that the source parameter is const char*, meaning that the source string is read-only and you cannot modify it.

Return Value of strcat()

The function returns a pointer to the resulting concatenated string, which is the destination parameter.

It is important to note that the destination string must have enough space to hold the concatenated string. Otherwise, the behavior of the function remains undefined. Now that may result in a segmentation fault or buffer overflow.

There are more similar articles to explore below. 

Compare Strings with strcmp Function in C
Compare Strings with strcmp Function in C
The strcmp function in C is used to compare two strings and determine whether they are equal or not. This article explains the syntax and usage of the strcmp function...read more
Understanding Strchr() Function in C
Understanding Strchr() Function in C
This blog explains the strchr() Function in C programming with the help of examples. Let’s understand!
All About Strlen() Function in C
All About Strlen() Function in C
Unveil the strlen() function in C programming, diving deep into its syntax, practical implementations, and code samples. Learn how to effectively utilise strlen() to determine string lengths, manipulate strings, and...read more

C Examples of strcat() Function 

Example 1: Concatenating two strings


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
const char str2[] = "learners!";
strcat(str1, str2); // Concatenate str2 to str1
printf("%s", str1); // Output: "Hello, world!"
return 0;
}
Copy code

Output

In this example, str1 initially contains the string “Hello, “, and str2 contains the string “learners!”. The strcat() function concatenates str2 to str1, resulting in the string “Hello, learners!”.

Example 2: Concatenating multiple strings


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
const char str2[] = "how are you";
const char str3[] = "?";
strcat(str1, str2); // Concatenate str2 to str1
strcat(str1, str3); // Concatenate str3 to str1
printf("%s", str1); // Output: "Hello, world!"
return 0;
}
Copy code

Output

In this example, three strings are used: str1, str2, and str3. The strcat() function is used twice to concatenate str2 and str3 to str1, resulting in the string “Hello, how are you?”. Finally, the concatenated string is printed using printf() function.

Learn more about C and its functions in the videos in the compiled playlist.

 

Example 3: Concatenating strings using loop


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "The numbers are: ";
int numbers[] = { 1, 2, 3, 4, 5 };
int i;
for (i = 0; i < 5; i++) {
char str2[5];
sprintf(str2, "%d", numbers[i]); // Convert integer to string
strcat(str1, str2); // Concatenate str2 to str1
if (i < 4) {
strcat(str1, ", "); // Add comma and space between numbers
}
}
printf("%s", str1); // Output: "The numbers are: 1, 2, 3, 4, 5"
return 0;
}
Copy code

Output

In this example, an array of integers create a string that lists the numbers. The sprintf() function converts each integer to a string (str2), which then concatenates to str1 using strcat() function. A comma and space add between each number using an if statement. Finally, the concatenated string is printed using printf() function.

Example 4: Concatenating a string with a character array


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char name[] = "John";
strcat(str1, name); // Concatenate name to str1
printf("%s", str1); // Output: "Hello, John"
return 0;
}
Copy code

Output

In this example, str1 initially contains the string “Hello, “, and name contains the character array “John”. The strcat() function concatenates name to str1, resulting in the string “Hello, John”. Finally, the concatenated string prints using printf() function.

Example 5: Concatenating strings of different lengths


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Great ";
char str2[] = "Life";
strcat(str1, str2); // Concatenate str2 to str1
strcat(str1, "!"); // Add exclamation mark to str1
printf("%s", str1); // Output: "Hello, world!"
return 0;
}
Copy code

Output

In this example, str1 initially contains the string “Great “, and str2 contains the string “Life”. The strcat() function concatenates str2 to str1, resulting in the string “Great Life”. Finally, an exclamation mark is added to the end of str1 using another call to strcat(), resulting in the string “Great Life!”.

Endnotes

The strcat() function is a useful tool in C programming for concatenating strings. It allows joining two or more strings together, which are useful in various programming tasks. In order to use the strcat() function correctly, it is important to understand how it works and how to properly pass arguments to it. Hope you have learnt how to do so by now. 

Meanwhile, do explore all the levels of C Programming courses if you need more guidance on such topics.

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

Comments

(1)

Thanks. It really gives me a satisfaction about the topic.

Reply to Dhak Bahadur Thapa