#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *ch1 = "hello";
char *ch2 = "world";
char *temp = (char *)malloc(strlen(ch1) + strlen(ch2));
strcat(temp,ch1);
strcat(temp,ch2);
printf("Test temp = %s\n",temp);
strcat(temp,"\'");
printf("Test1 temp = %s\n",temp);
free(temp);
char *temp1 = {0};//必须初始化
sprintf(temp1,"%s%s from \'li\'\n",ch1,ch2);
printf("Test temp1 = %s",temp1);
return 0;
}
输出结果: Test temp = hello world
Test1 temp = hello world'
Test temp1 = hello world from 'li'