get 和 scanf 有什么区别? [英] What's the difference between gets and scanf?

查看:33
本文介绍了get 和 scanf 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果代码是

scanf("%s
",message)

对比

gets(message)

有什么区别?似乎他们都得到了消息的输入.

解决方案

[参考您的特定场景] 的基本区别,

  • scanf() 在遇到 whitespacenewlineEOF 时结束接受输入p>

  • gets() 将空格视为输入字符串的一部分,并在遇到 newlineEOF 时结束输入.

但是,为了避免缓冲区溢出错误并避免安全风险,使用fgets()更安全.

If the code is

scanf("%s
",message)  

vs

gets(message)

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

解决方案

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

这篇关于get 和 scanf 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆