![]() |
VOOZH | about |
Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C.
The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example:
Hello Geeks
The strcat() function is specifically designed to concatenate two strings. It appends the second string (source string) to the end of the first string (destination string)
Note: The destination string should have enough space to store the concatenated string. Otherwise, a segmentation fault may occur.
There are also a few other methods in C to concatenate two strings. Some of them are as follows:
Table of Content
The sprintf() function is used to print / store a formatted string to a string buffer. It can be used for concatenating two strings by using the source string as formatted string and destination string as string buffer.
Hello Geeks
The memmove() function can be used to copy the whole block of memory occupied by source string at the end of destination string effectively concatenating the two strings.
Hello Geeks
Note: The memcpy() function can also be used in place of memmove() if the source and destination string does not overlap.
Use a loop to copy each character of source string at the end of the destination string. The end of the destination string can be found by using strlen() function.
Hello Geeks