Difference between scanf() and gets() in C
scanf()
- It is used to read the input(character, string, numeric data) from the standard input(keyboard).
- It is used to read the input until it encounters a whitespace, newline or End Of File(EOF).
For example see the following code:
// C program to see how scanf() // stops reading input after whitespaces #include <stdio.h> int main() { char str[20]; printf("enter something\n"); scanf("%s", str); printf("you entered: %s\n", str); return 0; } |
Here the input will be provided by the user and output will be as follows:
Input: Geeks for Geeks Output: Geeks Input: Computer science Output: Computer
gets
- It is used to read input from the standard input(keyboard).
- It is used to read the input until it encounters newline or End Of File(EOF).
// C program to show how gets() // takes whitespace as a string. #include <stdio.h> int main() { char str[20]; printf("enter something\n"); gets(str); printf("you entered : %s\n", str); return 0; } |
Here input will be provided by user as follows
Input: Geeks for Geeks Output: Geeks for Geeks Input: Computer science Output: Computer science
The main difference between them is:
- 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.
- scanf can read multiple values of different data types whereas gets() will only get character string data.
The difference can be shown in tabular form as follows:
| scanf() | gets() |
|---|---|
| when scanf() is used to read string input it stops reading when it encounters whitespace, newline or End Of File | when gets() is used to read input it stops reading input when it encounters newline or End Of File. It does not stop reading the input on encountering whitespace as it considers whitespace as a string. |
| It is used to read input of any datatype | It is used only for string input. |
How to read complete sentence from user using scanf()
Actually we can use scanf() to read entire string. For example we can use %[^\n]s inside scanf() to read entire string.
// C program to show how to read // entire string using scanf() #include <stdio.h> int main() { char str[20]; printf("Enter something\n"); // Here \n indicates that take the input // until newline is encountered scanf("%[^\n]s", str); printf("%s", str); return 0; } |
The above code reads the string until it encounters newline.
Examples:
Input: Geeks for Geeks Output: Geeks for Geeks Input: Computer science Output: Computer science
Recommended Posts:
- Problem with scanf() when there is fgets()/gets()/scanf() after it
- Use of & in scanf() but not in printf()
- Why to use fgets() over scanf() in C?
- Why "&" is not used for strings in scanf() function?
- Cin-Cout vs Scanf-Printf
- Return values of printf() and scanf() in C/C++
- scanf() and fscanf() in C - Simple Yet Poweful
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- Difference between JSP and ASP
- What is the difference between GUI and CUI?
- What's difference between MMU and MPU?
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Difference between ++*p, *p++ and *++p
- Difference between C and C++
- Difference between while(1) and while(0) in C language
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



