C strcat() Function Tutorial

  • Post author:
  • Post category:C

In this section, we will learn what the strcat() function is and how to use it in C.

String Concatenation in C: strcat() Function

Sometimes we have two character-strings and we want to concatenate them. This is where we can use the `strncat` function to do the job.

The prototype of this function exists in the `string.h` header file and so we need to include that header file if we want to use the function.

strcat() Function Syntax

Here’s the prototype of the `strcat` function:

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

strcat() Function Parameters

  • The first parameter of this function is the address of the first character-string that we want to attach a new character-string to the end of it.
  • The second parameter is the address of the character-string that we want to get a copy of it and attach it to the end of the first character-string.

Note: the second character-string doesn’t change in this operation.

Also the first character-string should have enough space to hold the second character-string. Otherwise there’s a chance that overflow might happen on the first character-string.

strcat() Function Return Value

The return value of this method is the address of first character-string (first parameter).

Example: append string in C

#include <stdio.h>
#include <string.h>
int main() {

    char array[50] = "Hi, ";

    strcat(array, "this is for test");

    puts(array);
    return 0;
}

Output:

Hi, this is for test

Leave a Reply