3 Main Difference Between Scanf () Vs. Gets () In C

SHARE

Gets()

Gets is used to receive inputs from the keyboard till encountering a newline or End Of File (EOF). The whitespace is considered as part of the input.

gets() receives a string from the keyboard. scanf() has some limitations while a receiving a string of characters.There is no way to enter a multi-word string into a single variable. gets()function gets a string from the keyboard and it is terminated when an Enter Key is hit.Thus, spaces and tabs are perfectly acceptable as part of the input string. gets() gets a newline (\n) terminated string of characters from the keyboard and replaces the \n with a \0.

Scanf ()

Scanf() allows us to enter data from keyboard that will be formatted in a certain way.

The genaral form of scanf() statement is as follows :

Note that we are sending addresses of variables (addresses are obtained by using ‘&’ the ‘address of’ operator) to scanf() function. This is necessary because the values received from keyboard must be dropped into variables corresponding to these addresses. The values that are supplied through the keyboard must be separated by either blank(s), tab(s), or newline(s). Do not include these escape sequences in the format string.

The Difference

The main difference between Scanf () and gets () in C is:

  1. Scanf ()can read multiple values of different data types whereas gets () will only get character string data. Gets () can be used to read one string at a time.
  2. Scanf () reads input until it encounters whitespace, newline or End Of File (EOF) whereas gets () reads input until it encounters newline or End Of File (EOF), gets () does not stop reading input when it encounters whitespace instead it takes whitespace as a string.
  3. Scanf () function takes the format string and list of addresses of variables e.g scanf(“%d”, & number) whereas gets () function takes the name of the variable to store the received value e.g gets(name).

Similarities Between scanf () and gets ()

  • Both scanf () and gets () are functions provided by C programming language.
  • Both scanf () and gets () can be used to get input from the standard input.
  • Both scanf () and gets () should include header file stdio.h to use these functions.

Scanf () Vs. gets ()  In Tabular Form

BASIS OF COMPARISON   Scanf () Gets ()
Reading Of Input Scanf () reads input until it encounters whitespace, newline or End Of File (EOF) Gets () reads input until it encounters newline or End Of File (EOF), gets () does not stop reading input when it encounters whitespace instead it takes whitespace as a string.  
Reading Of Values & Characters Scanf () can read multiple values of different data types. Gets () will only get character string data. Gets () can be used to read one string at a time.  
How It Operates  Scanf () function takes the format string and list of addresses of variables e.g scanf(“%d”, & number) Gets () function takes the name of the variable to store the received value e.g gets(name).