Skip to main content

Posts

Showing posts with the label The Crazy Programmer fgets() vs scanf() in C fgets() vs scanf() in C The Crazy Programmer

fgets() vs scanf() in C Nitish Agarwal The Crazy Programmer

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 specif