What's the difference between gets and scanf?

Shihe Zhang picture Shihe Zhang · Oct 28, 2014 · Viewed 61.4k times · Source

If the code is

scanf("%s\n",message)  

vs

gets(message)

what's the difference?It seems that both of them get input to message.

Answer

Sourav Ghosh picture Sourav Ghosh · Oct 28, 2014

The basic difference [in reference to your particular scenario],

  • scanf() ends taking input upon encountering a whitespace, newline or EOF

  • gets() considers a whitespace as a part of the input string and ends the input upon encountering newline or EOF.

However, to avoid buffer overflow errors and to avoid security risks, its safer to use fgets().