Appending strings in C

前端 未结 6 1849
无人共我
无人共我 2020-12-12 02:21

How do I combine multiple strings. For example,

char item[32];
scanf(\"%s\", item);
printf(\"Shopping list: %s\\n\", item); //I want to combine this string          


        
6条回答
  •  半阙折子戏
    2020-12-12 02:52

    use strcpy and strcat

    char item[] = "Shopping list";
    char hw[] = "To do list: Sleep \n";
    char* itemhw;
    itemhw = malloc(strlen(item)+strlen(hw)+1);
    strcpy(itemhw, item);
    strcat(itemhw, hw); 
    free(itemhw);
    

提交回复
热议问题