What function we use , to accept multi word string ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What function we use , to accept multi word string ?

scanf() stops when space is encountered

3rd Jun 2020, 2:12 AM
VISHAL JAYMANGAL DEDAVAT
4 Answers
+ 7
Use fgets(), never use gets() - it doesn't have size arguments and can overrun the string buffer which makes it unsafe to use. It was deprecated in C99 and removed from C11. There is one thing to remember with fgets(). It stops at EOF or a newline, and the newline is added to the string. So you may have to remove it before doing string comparisons, file access, printing, etc.
3rd Jun 2020, 3:15 AM
Gen2oo
Gen2oo - avatar
3rd Jun 2020, 3:08 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
If you still want to use scanf() function, use it with a new line format specifier. It's scanf("%[^\n]s", str). Here, you'll get all the characters in the string before the "\n" character. But I recommend the fgets() function.
3rd Jun 2020, 5:48 AM
ogoxu
ogoxu - avatar
+ 2
3rd Jun 2020, 2:37 AM
Sandra Meyer
Sandra Meyer - avatar