gpt4 book ai didi

c - 在 C 中追加字符串

转载 作者:行者123 更新时间:2023-12-01 09:03:42 25 4
gpt4 key购买 nike

如何组合多个字符串。例如,

char item[32];
scanf("%s", item);
printf("Shopping list: %s\n", item); //I want to combine this string
char string_2[] = "To do list: Sleep\n"; // with this string
char hw[32];
scanf("%s", hw);
printf("Homework: %s\n", hw); // and this string

所以他们会像下面这样打印,

购物 list :(项目)

待办事项: sleep

作业:(hw)


但是我不想像上面的代码那样单独给printf命令,而是把字符串组合起来,在最后调用printf/


我怎么能做到这一点,因为我不能将这样的东西保存到一个单独的字符串中,char string1 = ("Shopping list: %s\n", item)

最佳答案

连接 字符串,最好使用 strcat功能。

但请确保您连接的目标字符串足够大以容纳所有内容(包括终止 '\0' 字符)。

关于c - 在 C 中追加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12776005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com