为什么不推荐使用gets()?

net*_*mer 2 c c-strings include

gets()在我的代码中使用时,编译器大喊

warning: the 'gets' function is dangerous and should not be used.`
Run Code Online (Sandbox Code Playgroud)

warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638)
[-Wdeprecated-declarations]
Run Code Online (Sandbox Code Playgroud)

有什么具体原因吗?

Sou*_*osh 5

有人可以解释为什么编译器会这样显示......?

是的,因为该gets()函数很危险,因为它存在缓冲区溢出问题。任何人都应该避免使用它。

此外,关于带有 的警告-Wdeprecated-declarationsgets()不再是C标准的一部分[C11以后]。所以,C 库编译器没有束缚,以支持更多。将来可以删除它。为了警告开发人员潜在的陷阱并阻止进一步使用gets(),编译器##会发出警告消息。


(##) 迂腐,警告不是由编译器 ( gcc) 自己生成的,而是由in 的pragma实现上的或属性引起gets()glibc,导致编译器发出警告。[礼貌,FUZxxl,来自欺骗答案。]