+ 2

How to take string input in c with unknown length?

I want to take string as an input in c but I am not aware of the length, how do I take input without mentioning the length?

28th Dec 2021, 1:43 PM
Mee_d
6 odpowiedzi
+ 3
Strings in c are character arrays . since arrays are fixed length, you must be set length at compile time. You must mention length. (If you want you can extend with dynamic initialization techniques.. ) In C++, you can do : string s; cin>>s;
28th Dec 2021, 2:28 PM
Jayakrishna 🇮🇳
+ 2
Ohk, I'll first complete the memory management section then try understanding this. Thank you Martin Taylor and Jayakrishna🇮🇳 for the quick replies and the help!
28th Dec 2021, 8:24 PM
Mee_d
+ 1
Thank you Martin Taylor, I am a beginner, if you could explain in brief how the code works using dynamic memory allocation would be great.
28th Dec 2021, 5:54 PM
Mee_d
0
Thank you Jayakrishna🇮🇳 what is the maximum length that I can take?
28th Dec 2021, 4:26 PM
Mee_d
0
It's upto your requirement. Suppose if you taking a name, generally 20 chars enough, so take as char name[20]; or you can take less or more, with not to waste memory or not less of memory to store name value.
28th Dec 2021, 4:33 PM
Jayakrishna 🇮🇳
0
What if the input is a paragraph?
28th Dec 2021, 5:03 PM
Mee_d