Name

strcat

Synopsis

Appends one string to another

#include <string.h>
char *strcat( char * restrict s1, const char * restrict s2 );

The strcat() function copies the string addressed by the second pointer argument, s2, to the location following the string addressed by the first pointer, s1. The first character of s2 is copied over the terminating null character of the string addressed by s1.The function returns the value of its first argument, which points to the concatenated string.

There is no limit to the number of characters strcat() may write before it encounters a null character in the source string. It is up to you the programmer to make sure that there is enough storage available at the destination to accommodate the result. You should consider using strncat() instead to reduce the risk of buffer overflows. You must also make sure that the locations that strcat() reads from and writes to do not overlap.

Example

typedef struct { char  lastname[32];
  char  firstname[32];
  _Bool ismale;
} Name;

char displayname[80];
Name *newName = calloc( 1, sizeof(Name) );

/* ... check for calloc failure; read in the name parts ... */

strcpy( displayname, ( newName->ismale ? "Mr. " : "Ms. " ) );strcat( displayname, newName->firstname );
strcat( displayname, " " );
strcat( displayname, newName->lastname );

puts( displayname );

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.