16 c
当我使用gets函数时,gcc给了我一个警告:
warning:the `gets' function is dangerous and should not be used.
Run Code Online (Sandbox Code Playgroud)
为什么获取和放置功能危险?
小智 48
如果您有这样的代码:
char s[10];
gets( s );
Run Code Online (Sandbox Code Playgroud)
并且在程序运行时输入超过10个字符,您将溢出缓冲区,从而导致未定义的行为.gets()函数无法阻止您键入字符,因此应该避免.相反,您应该使用fgets(),它允许您限制读取的字符数,以便缓冲区不会溢出:
char s[10];
fgets( s, 10, stdin );
Run Code Online (Sandbox Code Playgroud)
puts()函数是完全安全的,前提是您输出的字符串以空值终止.