您的位置:首页 > 编程语言 > C语言/C++

C语言,字符串的拼接.strcat()和sprintf()函数之间的比较

2018-01-18 16:46 387 查看
#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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: