scanf函数和scanf_s的区别 [英] Difference between scanf and scanf_s

查看:229
本文介绍了scanf函数和scanf_s的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想问的是这是什么two.If之间的差异有any.In大学,我一直教导,我用scanf函数,但在我个人计算机视觉工作室不断发出这样的警告。

So I want to ask what is the difference between this two.If there is any.In the university I have been taught and I am using scanf,but at my personal computer the visual studio keeps sending this warning.

 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead.

我不得不改变所有scanf函数来scanf_s或程序将不建。
(我用的MS Visual Studio的最新版本从2013)

And I have to change all scanf to scanf_s or the program won't build. (I am using the newest version of ms visual studio from 2013)

推荐答案

这是专属于微软编译器的功能。

It is a function that belongs specifically to the Microsoft compiler.

scanf函数原来只是读取任何控制台输入您键入并将其分配给一个变​​量的类型。

scanf originally just reads whatever console input you type and assign it to a type of variable.

如果您有一个名为 FIRST_NAME [5] 一个数组,并使用 scanf函数为亚历克斯,有没问题。如果你有相同的数组,并指定亚历山大,你可以看到它超过了5个插槽,数组包含,所以C还是会写上不属于该数组的内存,它可能会或可能不会崩溃程序根据如果事情试图访问,并在不属于该FIRST_NAME内存插槽写。这是 scanf_s 用武之地。

If you have an array called first_name[5] and you use scanf for "Alex", there is no problem. If you have the same array and assign "Alexander", you can see it exceeds the 5 slots that the array contains, so C will still write it on memory that doesn't belong to the array and it might or might not crash the program, depending if something tries to access and write on that memory slot that doesn't belongs to first_name. This is where scanf_s comes in.

scanf_s 有一个参数(参数),您可以指定缓冲区的大小和实际控制输入的限制,所以你不死机整个建筑。

scanf_s has an argument(parameter) where you can specify the buffer size and actually control the limit of the input so you don't crash the whole building.

这篇关于scanf函数和scanf_s的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆