C语言 scanf 和 scanf_s 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21434735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between scanf and scanf_s
提问by Tony Andreev
What is the difference between scanfand 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和 和有scanf_s什么区别?在大学里,我受过教,我正在使用scanf,但在我的个人计算机上,Visual Studio 不断发送此警告。
error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead.
And I have to change all scanfto scanf_sor the program won't build.
(I am using Visual Studio 2013)
而且我必须全部更改scanf为scanf_s否则程序将无法构建。(我使用的是 Visual Studio 2013)
回答by ironslab
It is a function that belongs specifically to the Microsoft compiler.
它是一个专门属于 Microsoft 编译器的函数。
scanforiginally 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 scanffor "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_scomes in.
如果你有一个名为阵列first_name[5],并使用scanf了“亚历克斯”,是没有问题的。如果您有相同的数组并分配“Alexander”,您可以看到它超过了数组包含的 5 个插槽,因此 C 仍会将其写入不属于该数组的内存中,并且它可能会或可能不会使程序崩溃,取决于是否有东西试图访问和写入不属于 first_name 的内存插槽。这是scanf_s进来的地方。
scanf_shas 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有一个参数(参数),您可以在其中指定缓冲区大小并实际控制输入的限制,这样您就不会使整个建筑物崩溃。
回答by pmg
scanf_s()is not described by the C99 Standard (or previous ones).
scanf_s()C99 标准(或以前的标准)没有描述。
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()作为扩展提供的附加功能并且不介意失去可移植性,请检查您的编译器文档。
回答by DanDan01010
What you can do to avoid this error is to paste the thing between the <>: <_CRT_SECURE_NO_WARNINGS> to a place. 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. then in preprocessor definitions, after everything, add a semicolon and paste the thing in. then press apply and ok. Your problem should be solved.
为了避免这个错误,你可以做的是将 <>: <_CRT_SECURE_NO_WARNINGS> 之间的东西粘贴到一个地方。要到达该位置,请在解决方案资源管理器中右键单击您的项目,然后单击属性。然后转到配置属性,然后是 c/c++,然后是预处理器。然后在预处理器定义中,在所有内容之后,添加一个分号并将其粘贴进去。然后按应用并确定。你的问题应该得到解决。

