My understanding.... scanf() is useful if user input is without spaces, be it a number of string. if user input has spaces (like multiple words) then use gets() as sir suggested. if you want to restrict the input to the exact number of bytes you are declaring then use fgets(str,sizeof(str),stdin);
2499
0
3974
Support Team
Aug 25
Your understanding is almost correct, but let me clarify a few points:
* scanf() is mainly used when the input does not contain spaces, such as numbers or single words.
* gets() should not be used, because it is unsafe and has been removed from modern C standards (it can cause buffer overflows).
* If the input may contain spaces (like full sentences) or if you want to restrict the input size safely, you should use fgets(str, sizeof(str), stdin);.
So, the safe and recommended practice is to use scanf() for simple inputs, and fgets() for strings with spaces or controlled buffer size.