Difference between scanf() and gets() in C


In C language both scanf() and gets() functions are defined to get input from external source and pass to system as input. Now there is some characteristics difference between both the functions.

Following are the important differences between scanf() and gets() in C −

Sr. No.Keyscanf() functiongets() function
1DefinitionThe scanf() function can read input from keyboard and stores them according to the given format specifier. It reads the input till encountering a whitespace, newline or EOF.On other hand gets() function is used to receive input from the keyboard till it encounters a newline or EOF. The whitespace is considered as a part of the input.
2WhiteSpaceIn scanf() function white-space is not considered as input character and also it stops reading input from external source if any white-space is encountered in between.On other hand in get() function white-space is considered as input character and also it does stops reading input from external source if any white-space encountered in between it continue its reading from input source.
3Syntaxscanf() function takes the format string and list of addresses of variables. e.g. scanf(“%d”, &number);On other hand get() function takes the name of the variable to store the received value. e.g. gets(name);
4DataTypescanf() function can read multiple values of different data types.However on other hand get() function will only get character string data.

Updated on: 09-Jun-2020

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements