以下是使用snprintf将两个字符串拼接的示例代码:

#include <stdio.h>

int main() {
    char str1[] = "Hello, ";
    char str2[] = "world!";
    char result[50];
    int len = snprintf(result, 50, "%s%s", str1, str2);
    printf("The concatenated string is: %s\n", result);
    printf("The length of the concatenated string is: %d\n", len);
    return 0;
}

输出:

The concatenated string is: Hello, world!
The length of the concatenated string is: 13

在此示例中,我们首先声明两个字符串str1str2,并使用snprintf函数将它们拼接到一个名为result的字符数组中。我们指定了拼接的最大长度为50,以避免缓冲区溢出。最后,我们打印出拼接后的字符串和它的长度。

snprintf输出两个字符串拼接

原文地址: https://www.cveoy.top/t/topic/bs1a 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录