字符串拼接

基本类型 / 字符串

char s1[] = "three";
char s2[] = "two";

//the buffer must be big enough for the whole result
char s3[64];
strcpy(s3, s1);
strcat(s3, ", ");
strcat(s3, s2);
//s3 is "three, two"

//strncat never writes past the buffer
strncat(s3, ", one", sizeof s3 - strlen(s3) - 1);
//s3 is "three, two, one"

char sGo[64];
snprintf(sGo, sizeof sGo, "%s, %s", s3, "go!");
//sGo is "three, two, one, go!"

//adjacent literals are joined by the compiler
const char *literal = "three, " "two";
//literal is "three, two"

printf("sGo is %s\n", sGo);
printf("literal is %s\n", literal);