让我们说我有:
printf("Hello, my name is %s\n", name_variable);
我想保存printf
在char数组中生成的字符串,以便我可以write
在某处。这样做的一个解决方案是声明char
数组,然后在其上使用strcpy
和strcat
。我想知道是否有一种更优雅的方式。
答案 0 :(得分:3)
答案 1 :(得分:1)
请查看[snprintf](http://www.cplusplus.com/reference/cstdio/snprintf/]
此函数能够将值存储在字符串
中代码如此:
char s[1024]; // or some other suitable value
snprintf(s, 1024, "Hello, my name is %s\n", name_variable);
printf("Here it is %s\n", s);