How can I read an input string of unknown length?

前端 未结 10 1318
逝去的感伤
逝去的感伤 2020-11-22 07:56

If I don\'t know how long the word is, I cannot write char m[6];,
The length of the word is maybe ten or twenty long. How can I use scanf to ge

10条回答
  •  既然无缘
    2020-11-22 08:38

    Take a character pointer to store required string.If you have some idea about possible size of string then use function

    char *fgets (char *str, int size, FILE* file);`
    

    else you can allocate memory on runtime too using malloc() function which dynamically provides requested memory.

提交回复
热议问题