Difference between scanf() and fgets()

Biswajyoti Das picture Biswajyoti Das · Aug 9, 2009 · Viewed 59.9k times · Source

I want to know what is the difference between fgets() and scanf(). I am using C as my platform.

Answer

Jonathan Leffler picture Jonathan Leffler · Aug 9, 2009

There are multiple differences. Two crucial ones are:

  • fgets() can read from any open file, but scanf() only reads standard input.
  • fgets() reads 'a line of text' from a file; scanf() can be used for that but also handles conversions from string to built in numeric types.

Many people will use fgets() to read a line of data and then use sscanf() to dissect it.