strcat() in C: Concatenate Strings

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.

Syntax Top ↑

strcat(destination, source);

Basic strcat() Example Top ↑

#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

Append More Than One String Top ↑

#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;
}

Check Capacity Before Concatenating Top ↑

#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;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer