In C language, the strcat function is used to append one string to the end of another;
strcat Function Header
The header file for the strcat function is <string.h>. Before using the strcat function, make sure to include this header file in your C code:
#include <string.h>
strcat Function Prototype
char *strcat(char * restrict s1,
const char * restrict s2);
The strcat function appends a copy of the string pointed to by s2 (including the terminating null character '\0') to the end of the string pointed to by s1.
The starting character of s2 will overwrite the null character at the end of s1. If copying occurs between overlapping objects, the behavior is undefined.
Parameter Description
s1: Pointer to the destination string (the modified string), which should have enough space to hold the appended content;s2: Pointer to the source string (the string to be appended);
Return Value
The strcat function returns a pointer to the destination string, which is the value of s1;
strcat Example Code
Using strcat to Concatenate Strings
#include <stdio.h>
#include <string.h>
int main() {
char s1[50] = "Hello,";
char s2[] = " World!";
strcat(s1, s2);
printf("%s\n", s1);
return 0;
}
Program Output
Hello, World!