zl程序教程

您现在的位置是:首页 >  其他

当前栏目

为什么在c语言中使用gets函数是危险的

语言 函数 为什么 危险 使用
2023-09-27 14:28:10 时间

 

 

If you have code like this:

char s[10];
gets( s );

and you type in more than 10 characters when the program is run, you will overflow the buffer, causing undefined behaviour. The gets() function has no means of preventing you typing the characters and so should be avoided. Instead you should use fgets(), which allows you to limit the number of characters read, so that the buffer does not overflow.:

char s[10];
fgets( s, 10, stdin );