C strcat()
Overview
The strcat() function which defined under the string.h header file in c concatenates two strings and returns a string in which the two strings are appended. The function takes the source and destination strings as parameters and returns the destination string where the destination and source strings are appended.
For example, if we pass the strings "I love" and "coding" to the strcat in c, the strcat function in c returns "I love coding".
What is C strcat()?
Two strings in C can be concatenated using the strcat function in c. The strcat in c is included in the string.h header file. The strcat function in C takes two parameters namely destination (dest) and source(src) pointing to the string. The source string will be concatenated to the destination string and the function returns the destination string.
How to use C strcat()
In the below program two strings are concatenated using a custom-made function mystrcat().
Output:
Explanation:
In the above code,
- We are concatenating two strings string1 and string2.
- The string Scaler is stored in the string1 variable.
- The string Topics is stored in the string2 variable.
- We created two pointers str1 and str2 which point to string1 and string2 respectively.
- We also created a function called mystrcat which takes two string pointers and return a string pointer.
- In the mystrcat function, A for loop runs through the string str1 and increments the value of the counter (len)until a null character is encountered to reach the end of str1.
- Another for loop runs through the second string str2 until the null character is encountered and all the characters of str2 are added to the end of str1.
- The mystrcat function returns str1 string pointer.
- In this way, we can concatenate two strings and implement strcat() functionalities using a custom-made function.
Syntax of C strcat()
The syntax of the strcat function in C is as follows :
In the above syntax, pointers are used, or simply put the syntax can be as follows :
Parameters of C strcat()
The strcat in c takes two parameters namely dest (also known as destination) and src (also known as source).
dest
dest is the pointer pointing to the string that will be modified. The string pointed by the pointer src will be copied to the end of the string pointed by the dest pointer.
src
src is the pointer pointing to the string which needs to be appended to the dest string.
Return value of C strcat()
The strcat in c returns a pointer that points to the dest string.
Exceptions of C strcat()
The strcat in c can concatenate only strings. We cannot use the strcat function in C to concatenate any other data types other than strings.
Let us try to concatenate two integers using strcat in c :
Output:
Using strcat in C, we cannot concatenate two integers. As we can see that strcat refers to string concatenate. The function can concatenate only strings. Trying to concatenate anything other than a string will result in an error.
Example
Code:
Output:
Explanation:
In the above code, the string.h header file is included to use strcat(). Later we passed dest and src as parameters to the strcat in c. The strcat function in c appends the src string to the dest string resulting in the dest string which is modified.
More Examples
Let us look at a few more examples of strcat() in C.
- Concatenating to the string not having enough space. Code
Output
Explanation
In the above code, We included the string.h header file and used the correct syntax of the strcat function in c, Yet we got an error. This is because the string a has the size of 7, after concatenating the string, a becomes Hello world. Hello world has 11 characters but the size of a is just 7. Therefore we get an error. It is important to note that the size of the destination array should be greater than the size of the concatenated string.
Conclusion
- The strcat in C is included under the string.h header file is used to concatenate two strings.
- The strcat in C takes two parameters namely destination (dest) and source (src). The source string is concatenated at the end of the destination string.
- The strcat function in C returns the destination string.
- The strcat function in C works only with the strings.