sprintf拼接字符串的问题
想给一个字符串前面添加一些信息,先贴源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
char buf[100];
memset(buf, 0, sizeof(buf));
//先给定初始值
sprintf(buf, "%s", "test");
//拼构字符
sprintf(buf, "www.%s", buf);
printf(buf);
printf("\n");
return 0;
}
大家猜下结果是什么?
实际输出结果为:
www.www.
原因是什么,大家想想。
我的问题是除了通过增加临时变量外,还有没有其他的方法来实现该目标。