/* strings2_copy_concat.c */ #include #include #include int main() { char str1[12] = "Hello"; // explicit initialization char str2[] = "World"; // implicit initialization /* Define another string variable, and allocate memory for it 'on-the-fly' */ char * str3 = (char *) malloc ((strlen(str1)+strlen(str2)+2) * sizeof(int)); printf("str1 contains \"%s\"\n",str1); // note how a double quote needs to be formatted, \" printf("str2 contains \"%s\"\n",str2); strcat(strcat(str1, " "), str2); // build "Hello World" by concatenation strcpy(str3, str1); // copy "Hello World" to str3 printf("A space and str2 concantenated to str1 is \"%s\"\n", str1); printf("str1 copied to str3 is \"%s\"\n", str3); free(str3); /* use malloc() in combination with free() to free memory after you have used it */ return 0; }