首页 > 代码库 > 为什么在c语言中使用gets函数是危险的

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

 

 

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 );

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