C++ strcat()

strcat() prototype

char* strcat( char* dest, const char* src );

The strcat() function takes two arguments: dest and src. This function appends a copy of the character string pointed to by src to the end of string pointed to by dest. The null terminating character at the end of dest is replaced by the first character of src and the resulting character is also null terminated.

The behaviour is undefined if

  • the strings overlap.
  • the dest array is not large enough to append the contents of src.

It is defined in <cstring> header file.

strcat() Parameters

  • dest: Pointer to a null terminating string to append to.
  • src: Pointer to a null terminating string that is to be appended.

strcat() Return value

The strcat() function returns dest, the pointer to the destination string.

Example: How strcat() function works

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    char dest[50] = "Learning C++ is fun";
    char src[50] = " and easy";

    strcat(dest, src);
    
    cout << dest ;

    return 0;

}

When you run the program, the output will be:

Learning C++ is fun and easy
Did you find this article helpful?