strcat Function

Appends a copy of the source string to the end of the destination string.

Include

<string.h>

Prototype

char *strcat(char *s1, const char *s2);

Arguments

s1
null terminated destination string to copy to
s2
null terminated source string to be copied

Return Value

Returns a pointer to the destination string.

Remarks

This function appends the source string (including the terminating null character) to the end of the destination string. The initial character of the source string overwrites the null character at the end of the destination string. If the buffers overlap, the behavior is undefined.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  char buf1[50] = "We're here";
  char buf2[50] = "Where is the time?";

  printf("buf1 : %s\n", buf1);
  printf("\t(%d characters)\n\n", strlen(buf1));
  printf("buf2 : %s\n", buf2);
  printf("\t(%d characters)\n\n", strlen(buf2));

  strcat(buf1, buf2);
  printf("buf1 after strcat of buf2: \n\t%s\n",
          buf1);
  printf("\t(%d characters)\n", strlen(buf1));
  printf("\n");

  strcat(buf1, "Why?");
  printf("buf1 after strcat of \"Why?\": \n\t%s\n",
         buf1);
  printf("\t(%d characters)\n", strlen(buf1));
}

Example Output

buf1 : We're here
        (10 characters)

buf2 : Where is the time?
        (18 characters)

buf1 after strcat of buf2:
        We're hereWhere is the time?
        (28 characters)

buf1 after strcat of "Why?":
        We're hereWhere is the time?Why?
        (32 characters)