How to concatenate two strings in C language?

In C language, the library function strcat() can be used to concatenate two strings.

#include <stdio.h>
#include <string.h>

int main() {
    char str1[50] = "Hello ";
    char str2[] = "world!";
    
    strcat(str1, str2);
    
    printf("合并后的字符串:%s\n", str1);
    
    return 0;
}

The output yields:

合并后的字符串:Hello world!

The `str1` here is the target string to merge, while `str2` is the string to append to the end of `str1`. The `strcat()` function will append `str2` to the end of `str1`, and add a null character `\0` at the end of the merged string.

bannerAds