Shihe Zhang Shihe Zhang -3 years ago 237
C Question

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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download