| Functions | |||||||||||||||||||||||||||||||||||||||||
| Character manipulation | |||||||||||||||||||||||||||||||||||||||||
| Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String examination | |||||||||||||||||||||||||||||||||||||||||
| Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)(C11) | |||||||||||||||||||||||||||||||||||||||||
Defined in header <string.h> | ||
| (1) | ||
char*strcat(char*dest,constchar*src); | (until C99) | |
char*strcat(char*restrict dest,constchar*restrict src); | (since C99) | |
errno_t strcat_s(char*restrict dest, rsize_t destsz,constchar*restrict src); | (2) | (since C11) |
src to the end of the null-terminated byte string pointed to bydest. The charactersrc[0] replaces the null terminator at the end ofdest. The resulting byte string is null-terminated.src anddest and the terminating null character. The behavior is undefined if the strings overlap. The behavior is undefined if eitherdest orsrc is not a pointer to a null-terminated byte string.destsz) with unspecified values and that the following errors are detected at runtime and call the currently installedconstraint handler function:src ordest is a null pointerdestsz is zero or greater thanRSIZE_MAXdestsz bytes ofdestdest would not fit every character, including the null terminator, ofsrc)dest <strlen(dest)+strlen(src)+1 <=destsz; in other words, an erroneous value ofdestsz does not expose the impending buffer overflow.strcat_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<string.h>.Contents |
| dest | - | pointer to the null-terminated byte string to append to |
| src | - | pointer to the null-terminated byte string to copy from |
| destsz | - | maximum number of characters to write, typically the size of the destination buffer |
destdest is a null pointer ordestsz is zero or greater thanRSIZE_MAX).Becausestrcat needs to seek to the end ofdest on each call, it is inefficient to concatenate many strings into one usingstrcat.
strcat_s is allowed to clobber the destination array from the last character written up todestsz in order to improve efficiency: it may copy in multibyte blocks and then check for null bytes.
The functionstrcat_s is similar to theBSD functionstrlcat, except that
strlcat truncates the source string to fit in the destinationstrlcat does not perform all the runtime checks thatstrcat_s doesstrlcat does not make failures obvious by setting the destination to a null string or calling a handler if the call fails.Althoughstrcat_s prohibits truncation due to potential security risks, it's possible to truncate a string using bounds-checkedstrncat_s instead.
#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}
Possible output:
Hello World! ... Goodbye World!str = "Hello World! ... Goodbye World! ... ", r = 0str = "", r = 22
(C11) | concatenates a certain amount of characters of two strings (function)[edit] |
(C11) | copies one string to another (function)[edit] |
(C23) | copies one buffer to another, stopping after the specified delimiter (function)[edit] |
C++ documentation forstrcat | |