A function is a set of statements to perform a specific task. Without writing all statements in the same program, it can be divided into multiple functions. In programming, the user can define his own functions. There are also functions provided by programming languages. C language provides a number of functions, so the programmer can use them directly without implementing them from the beginning. Two such functions provided by C language are scanf and gets. This article discusses the difference between these two functions. The key difference between scanf and gets is that scanf ends taking input upon encountering a whitespace, newline or End Of File (EOF) whereas gets considers a whitespace as the part of the input string and ends the input upon encountering newline or EOF.
CONTENTS
1. Overview and Key Difference
2. What is scanf
3. What is gets
4. Similarities Between scanf and gets
5. Side by Side Comparison – scanf vs gets in Tabular Form
6. Summary
What is scanf?
The 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. The syntax is as follows.
scanf(“format string”, list of address of variables);
Refer the example given below to understand scanf.
According to the above program, the input getting from the keyboard is an integer, so the format specifier is %d. If it is getting a character value, the format specifier is %c. If getting a floating-point value, the format specifier is %f. The received input value should be stored in the number variable. Therefore, the address of variable number is passed into the scanf function. Now the number variable contains the value given by the user from the keyboard. Finally, we can print the number variable to check the value.
It is also possible to receive more than one value at a time.
The received inputs are stored in variable number1 and number2. Values can be checked by using printf.
What is gets?
gets function is using to receive input from the keyboard till encountering a newline or EOF. The whitespace is considered as a part of the input. The syntax for gets function is as follows.
gets(“where to store the string”);
If there is an error when receiving the string, the gets function will return a null value.
Refer the below example,
The input is received by the gets function and stored in variable word1. If the programmer used scanf instead of gets and input a string such as “hello world”, scanf will read the string as two strings because of the whitespace. But gets will read it as one string “hello world”.
What are the Similarities Between scanf and gets?
- Both are functions provided by C programming language.
- Both should include header file stdio.h to use these functions.
- Both can be used to get input from the standard input.
What is the Difference Between scanf and gets?
scanf vs gets |
|
scanf is a C function to read input from the standard input until encountering a whitespace, newline or EOF. | gets is a C function to read input from standard input until encountering a newline or EOF. It considers whitespace as a part of the input. |
Syntax | |
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); |
Flexibility | |
scanf can read multiple values of different data types. | gets() will only get character string data. |
Summary – scanf vs gets
scanf and gets are functions provided by the programming language C. User does not need to implement these functions from the beginning. They can directly use them in their programs. The difference between scanf and gets is that, scanf ends taking input upon encountering a whitespace, newline or End Of File (EOF) and gets considers a whitespace as the part of the input string and ends the input upon encountering newline or EOF. Using scanf or gets depends on the way to receive user input from the standard input which is the keyboard most of the time. scanf is more flexible than gets.
Download the PDF Version of scanf vs gets
You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between scanf and gets
Reference:
1.LearningLad., ‘Scanf() Function in C Programming Language Video Tutorials’ YouTube, YouTube, 23 Apr. 2013. Available here
2.LearningLad,’gets() String Input Function in C Programming Language Video Tutorial’,YouTube, YouTube, 6 May 2013. Available here