码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-09-06 19:56:15      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

 

 

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函数是危险的

标签:

原文地址:http://www.cnblogs.com/litifeng/p/5846813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!