C++ Standard Library C++ STL Library

C++ <cstring> - strcat() Function



The C++ <cstring> strcat() function is used to append a copy of the character string pointed to by source to the end of character string pointed to by destination. The terminating null character in destination is overwritten by the first character of source, and the resulting byte string is null-terminated.

The behavior is undefined if the destination is not large enough for the content of resulting byte string (including the null character), or if the strings overlap.

Syntax

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

Parameters

destination Specify pointer to the null-terminated byte string to append to.
source Specify pointer to the null-terminated byte string to copy from.

Return Value

Returns destination.

Example:

The example below shows the usage of strcat() function.

#include <iostream>
#include <cstring>
using namespace std;
 
int main (){
  char str[100] = "Hello ";
  strcat(str, "World!.");
  cout<<str<<"\n";  

  char str2[50] = " Programming is fun.";
  strcat(str, str2);
  cout<<str<<"\n";   
  return 0;
}

The output of the above code will be:

Hello World!.
Hello World!. Programming is fun.

❮ C++ <cstring> Library