Search code examples
cinputscanfgets

What's the difference between gets and scanf?


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.


Solution

  • 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().