What is the difference between gets() and scanf() in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 18

What is the difference between gets() and scanf() in c

What is the difference between gets() and scanf() in c

5th Mar 2019, 4:20 AM
Khalid Saifullah Fuad
Khalid Saifullah Fuad - avatar
4 Answers
+ 16
gets() considers a whitespace as a part of the string and ends the input upon encountering \n or EOF. On the other hand scanf() ends taking input upon encountering a whitespace, \n and EOF. scanf() allows to specifying the maximum character length to be scanned. Whereas in gets() it is not possible.
10th Mar 2019, 3:46 AM
Khalid Saifullah Fuad
Khalid Saifullah Fuad - avatar
+ 9
gets() will register a line of input as a string up until the user hits enter, including spaces scanf() will get input of the given format specifier up until it encounters a space or newline character
5th Mar 2019, 11:56 PM
boneSpider
boneSpider - avatar
+ 4
Lets learn the basic way.. scanf() is used to read a single character from the user.Example- scanf(%d,&num); this will ask the user to input a single number On the other hand, if we use gets() then the user can input a string(a bunch of characters) at a single go until you press the enter button.
9th Mar 2019, 3:06 PM
Furkan Ahmed
Furkan Ahmed - avatar
+ 2
The main difference between gets and scanf is that scanf reads input until it encounters newline, whitespace or end of line where as gets reads the inputs until it encounter newline or end of line. It does not stop reading the input when it encounter with whitespace.
11th Mar 2019, 8:15 AM
rajshivakoti
rajshivakoti - avatar