Fundamentals of C

Why scanf?
Why scanf?

Student
Fayaz   Aug 25

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);

Support Team
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.
REPLY

Student
Raja Balaji   14-Jan-22

the purpose of scanf is to make user-friendly rather than fixed.

Support Team
Support Team   17-Jan-22

scanf is pre-defined function to get the inputs from user it is a primary. What is fixed?
REPLY

Student
Jeganathan R   08-Jun-23

fixed is normal ( printf ) like a fixed value

Total Duration: 15:11:57