The strcat() function in C

This syntax of the strcat() function is:

Syntax: char* strcat (char* strg1, const char* strg2);

This function is used to concatenate two strings. This function accepts two arguments of type pointer to char or (char*), so you can either pass a string literal or an array of characters. The null character from the first string is removed then second string is appended at the end of the first string. It returns a pointer to the resulting string (strg1). Generally, the return value of strcat() is discarded.

Here are some examples:

char strg1[40] = "Hello";

/*
   returns a pointer (which is discarded) to the string literal
   "Hello World" and now strg1 contains "Hello World"
*/
strcat(strg1, " World"); 

/* 
  returns a pointer (which is discarded) to the string
  to "Hello World :)" and now strg1 contains "Hello World :)"
*/
strcat(strg1, " :)");

You should never pass a string literal as a first argument because if you do then strcat() function will try to modify a string literal which is an undefined behavior and may cause the program to crash.

strcat("Yello", " World"); // wrong

The behavior of strcat() is undefined when the size of the array pointed to by strg1 isn't long enough to accommodate all the characters from strg2. It is programmer's responsibility to make sure that size of the array pointed to by strg1 is long enough to accommodate all the characters from strg2.

The following program demonstrates how to use strcat() function.

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

int main()
{
    char strg1[40];
    char strg2[40];

    printf("Enter first string: ");
    gets(strg1);

    printf("Enter second string: ");
    gets(strg2);

    printf("\nConcatenating first and second string .. \n\n");
    strcat(strg1, strg2);

    printf("First string: %s\n", strg1);
    printf("Second string: %s", strg2);

    // signal to operating system program ran fine
    return 0;
}

Expected Output:

Enter first string: top
Enter second string: pot

Concatenating first and second string ..

First string: toppot
Second string: pot

Let's create our own version of strcat() function

char *my_strcat(char *strg1, char *strg2)
{
    char *start = strg1;

    while(*strg1 != '\0')
    {
        strg1++;
    }

    while(*strg2 != '\0')
    {
        *strg1 = *strg2;
        strg1++;
        strg2++;
    }

    *strg1 = '\0';
    return start;
}

How it works

my_strcat() function accepts two arguments of type pointer to char or (char*) and returns a pointer to the first string i.e strg1.

In line 3, we have assigned the pointer strg1 to start, this step is necessary otherwise we will lose track of the address of the beginning of the first string (strg1).

The job of the first while is loop is to move the pointer strg1 to the last character i.e '\0'. So that the second while loop can begin appending character at this position.

The second while loop appends character one by one from second string to first string. Since after first while loop strg1 is pointing to the null character of the first string, in the first iteration the statement:

*strg1 = *strg2;

appends first character from second string to the end of the first string (i.e in place of null character '\0'). Then strg1++ and strg2++ is incremented. This process keeps repeating until the null character is encountered in the second string(strg2).

At this point, the string pointed to by start still lacks one thing, the null character ('\0'). The statement:

*strg1 = '\0';

appends the null character at the end of the first string.

At last, the return statement returns the pointer to the first string to the calling function.