C String Concatenation: strcat()

The strcat() function is a commonly used string function in the C programming language. It is defined in the <string.h> header file and is used to concatenate (join) two strings together.

The syntax of strcat() function is as follows:

char* strcat(char* destination, const char* source);

Here, destination is a pointer to the destination string, and source is a pointer to the source string. The strcat() function appends the contents of the source string to the destination string.

It is important to note that the destination string must have enough space to hold the concatenated result. If the destination string is not large enough, it may result in a buffer overflow, leading to undefined behavior.

The strcat() function works by finding the null terminator ('\0') in the destination string and then copying the characters from the source string starting from that position, including the null terminator. It continues until the end of the source string is reached.

Here’s an example usage of strcat():

#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[] = " World!";
    strcat(str1, str2);
    printf("%s\n", str1);  // Output: Hello World!
    return 0;

In this example, str1 is the destination string, and str2 is the source string. The strcat() function is used to concatenate str2 to the end of str1, resulting in the string “Hello World!” being stored in str1. Finally, the concatenated string is printed using printf().