简体   繁体   English

scanf 和 scanf_s 的区别

[英]Difference between scanf and scanf_s

What is the difference between scanf and scanf_s ? scanfscanf_s什么scanf_s In the university I have been taught and I am using scanf , but at my personal computer Visual Studio keeps sending this warning.在大学里,我一直在教我并且我正在使用scanf ,但是在我的个人计算机上,Visual Studio 不断发送此警告。

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

And I have to change all scanf to scanf_s or the program won't build.而且我必须将所有scanf更改为scanf_s否则程序将无法构建。 (I am using Visual Studio 2013) (我使用的是 Visual Studio 2013)

It is a function that belongs specifically to the Microsoft compiler.它是一个专门属于 Microsoft 编译器的函数。

scanf originally just reads whatever console input you type and assign it to a type of variable. scanf最初只是读取您键入的任何控制台输入并将其分配给一种变量。

If you have an array called first_name[5] and you use scanf for "Alex", there is no problem.如果您有一个名为first_name[5]的数组,并且您将scanf用于“Alex”,则没有问题。 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.如果您有相同的数组并分配“Alexander”,您可以看到它超过了数组包含的 5 个插槽,因此 C 仍会将其写入不属于该数组的内存中,并且它可能会或可能不会使程序崩溃,取决于是否有东西试图访问和写入不属于 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_s有一个参数(参数),您可以在其中指定缓冲区大小并实际控制输入的限制,这样您就不会使整个建筑物崩溃。

scanf_s() is not described by the C99 Standard (or previous ones). C99 标准(或以前的标准scanf_s()未描述scanf_s() )。

If you want to use a compiler that targets C99 (or previous) use scanf() .如果要使用针对 C99(或以前)的编译器,请使用scanf()

For C11 Standard (and eventually later ones) scanf_s() is much harder to use than scanf() for improved security against buffer overflows.对于 C11 标准(以及最终的标准)来说, scanf_s()scanf()更难使用,以提高针对缓冲区溢出的安全性。

C11 fscanf_s() : http://port70.net/~nsz/c/c11/n1570.html#K.3.5.3.2 C11 fscanf_s() : http://port70.net/~nsz/c/c11/n1570.html#K.3.5.3.2

~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

If you have a C99 compiler with extras that provides scanf_s() as an extension and don't mind losing portability, check your compiler documentation.如果您有一个带有附加功能的 C99 编译器,它提供scanf_s()作为扩展并且不介意失去可移植性,请检查您的编译器文档。

What you can do to avoid this error is to paste the thing between the <>: <_CRT_SECURE_NO_WARNINGS> to a place.为了避免这个错误,你可以做的是将 <>: <_CRT_SECURE_NO_WARNINGS> 之间的东西粘贴到一个地方。 To get to the place right click on your project in the solution explorer and click on the properties.要到达该位置,请在解决方案资源管理器中右键单击您的项目,然后单击属性。 then go to the configuration properties, then the c/c++, then the preprocessor.然后转到配置属性,然后是 c/c++,然后是预处理器。 then in preprocessor definitions, after everything, add a semicolon and paste the thing in. then press apply and ok.然后在预处理器定义中,在所有内容之后,添加一个分号并将其粘贴进去。然后按应用并确定。 Your problem should be solved.你的问题应该得到解决。

BTW, the reason your compiler is giving you an error, and stopping, rather than the 'warning' the issue actually is, is that you must have your Visual Studio setup such that it will "treat all warnings as errors". 顺便说一句,你的编译器给你一个错误的原因,并且停止,而不是实际上是“警告”问题,你必须安装Visual Studio,以便“将所有警告视为错误”。 Change the configuration to let warnings be warnings, and you can then also just ignore this warning. 更改配置以使警告成为警告,然后您也可以忽略此警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM