This question already has an answer here:
- How to read a line from the console in C? 13 answers
I want to take a string from stdin but I don't want a static array of fixed size
I know that scanf need something where save the stdin input, but I can't do something like this:
char string[10]
scanf("%s",string);
becouse I need to know before how long will be the string in order to allocate the right memory space.
Can you help me to resolve this problem?
woooooooo
i'm still locked with this problem... I'm going mad
can you please give me the working code?
Do not use scanf, use fgets, which prevents too many characters from being read in.
fgets will return a '\n' at the end of the line, NULL when stdin closes or encounters an error.
Play with it first to see what it does. If you need to break the input line into fields you can use sscanf() on tmp, sscanf works just like scanf, but on strings.