+ 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?
6 Answers
+ 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;
+ 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!
+ 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.
0
Thank you Jayakrishnađźđł what is the maximum length that I can take?
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.
0
What if the input is a paragraph?