strcat() appends the complete source string to the end of a destination string. The destination array must already contain a valid null-terminated string and must have enough unused capacity for the result.
strcat(destination, source);
#include <stdio.h>
#include <string.h>
int main(void) {
char destination[30] = "welcome to ";
char source[] = "plus2net";
strcat(destination, source);
printf("%s\n", destination);
return 0;
}
Output
welcome to plus2net
#include <stdio.h>
#include <string.h>
int main(void) {
char message[60] = "Learn ";
strcat(message, "C ");
strcat(message, "programming ");
strcat(message, "with plus2net.");
printf("%s\n", message);
return 0;
}
#include <stdio.h>
#include <string.h>
int main(void) {
char destination[20] = "C: ";
char source[] = "strings";
size_t required = strlen(destination) + strlen(source) + 1;
if (required <= sizeof(destination)) {
strcat(destination, source);
printf("%s\n", destination);
} else {
printf("Destination is too small.\n");
}
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.