strcat, strcat_s

Header: <string.h>

  1. Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest. The character src[0] replaces the null terminator at the end of dest. The resulting byte string is null-terminated.

# Declarations

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

(until C99)

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

(since C99)

errno_t strcat_s(char *restrict dest, rsize_t destsz, const char *restrict src);

(since C11)

# Parameters

# Notes

Because strcat needs to seek to the end of dest on each call, it is inefficient to concatenate many strings into one using strcat.

strcat_s is allowed to clobber the destination array from the last character written up to destsz in order to improve efficiency: it may copy in multibyte blocks and then check for null bytes.

The function strcat_s is similar to the BSD function strlcat, except that

Although strcat_s prohibits truncation due to potential security risks, it’s possible to truncate a string using bounds-checked strncat_s instead.

# Example

#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
 
int main(void) 
{
    char str[50] = "Hello ";
    char str2[50] = "World!";
    strcat(str, str2);
    strcat(str, " ...");
    strcat(str, " Goodbye World!");
    puts(str);
 
#ifdef __STDC_LIB_EXT1__
    set_constraint_handler_s(ignore_handler_s);
    int r = strcat_s(str, sizeof str, " ... ");
    printf("str = \"%s\", r = %d\n", str, r);
    r = strcat_s(str, sizeof str, " and this is too much");
    printf("str = \"%s\", r = %d\n", str, r);
#endif
}

# See also