fgets() vs scanf() in C

In this article, we will take a look at two of the most basic functions used in the C language and subsequently, we will compare them on the basis of their usage.

Both fgets and scanf functions are used to take basic user inputs in the program. The major difference between these two functions is that the scanf function takes the input of any data type (int, float, char, double, etc.) but falls behind while taking string inputs. The reason for this behavior is that string inputs contain whitespaces and as soon as the scanf function encounters these whitespaces, it stops scanning further, thus making the function less accessible for string inputs.

While, the fgets function is used to take inputs of any data type (int, float, double, char, etc.) as well as string inputs containing whitespaces. One more major advantage of using fgets function is that it can even read input data from a specified text file or console, which the scanf function cannot. fgets function even allows us to specify the size of user input to be taken, thus making it a user-friendly function.

scanf() in C

The format to take user input using scanf function is given as:

The format specifier to take integer input is “%d”, for float input, it is “%f” and for string input, it is “%s”. Let us understand this concept using a sample program.



In this program, we have initialized an integer ‘a’, a float number ‘b’, and a string ‘c’. Note that the data type of string is char and the numerical part written after the string variable ‘c’ is the maximum length of the string, user can input.

As we can observe from the output, the scanf function stopped scanning the string input after it encountered whitespace and therefore, displayed only the first word of the string.

fgets() in C

To remove this error, fgets function comes in force. The format to take user input using fgets function is given as:

Here, stdin is a keyword used to take standard input from stdin stream. To understand this better, let us see a sample program.



Here, we notice that fgets function read the full string and didn’t stop scanning the string even after encountering whitespaces. Therefore, using fgets function for string input is advantageous and handy.

One more usage of fgets function is that it is also used to take input directly from a text file. Let us understand this using a sample program.

Consider a text file named ‘file.txt’ containing the line “Hello World!” saved in your computer system. Now, study the following program :


In this program, we created a pointer to a file ‘fp’ which is used to read the content from the file. The ‘r’ keyword in Line 3 is used to read the file. In line 4, the fgets function is used to take input from the file, which will print the string up to 20 characters. Remember to close the file after reading it at the end of the program using ‘fclose’ function.

Although scanf is mostly used to take user input, it is preferable to use fgets while taking string inputs as it makes the program error-free.

Leave a Comment

Your email address will not be published. Required fields are marked *