c - C中字符串连接的代码太多

标签 c string

我的代码中有很多 strcat 行。有没有更好的方法在 C 中连接字符串?

char material[50]; // it is defined before this code.
char result[10000];
strcpy(result, "// Assign new material to worldGlobe\n");
strcat(result, "shadingNode -asShader lambert -n ");
strcat(result, material);
strcat(result, ";\n");

最佳答案

您可以将格式字符串与 snprintf() 结合使用(与 sprintf() 相比安全):

snprintf(result, 10000,
    "// Assign new material to worldGlobe\nshadingNode -asShader lambert -n %s;\n",
    material);

关于c - C中字符串连接的代码太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8647307/

相关文章:

javascript - 将字符串和变量连接成 Jade 的变量名

c - 字符串中间的 '\0'在C中能被识别为字符串的结尾吗?

c++ - C/C++ 事件驱动监控非子程序的终止

sql - Postgres : Select the maximum integer substring of a column value

c# - C# 与 F# 中的默认排序

ios - 如何将Swift中的小数格式化为百分比

c - 如何在 C 中的用户定义后在程序中 append 文件名

c - 如何设置寄存器值以启用中断?

c - 有什么方法可以将结构类型传递给 c 函数

Char *(指针)函数