When compiling a C language project in VS 2012, if the scanf function is used, the following error will be prompted when compiling:
error C4996:’scanf’: This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. |
The reason is that Visual C++ 2012 uses more secure run-time library routines. For the new Security CRT functions (that is, those with the “_s” suffix), see:
” Security Enhanced Version of CRT Function “
The solution to this problem is given below:
Method 1: Replace the original old functions with new Security CRT functions.
Method 2: Use the following methods to block this warning:
1. Define the following macros in the precompiled header file stdafx.h (note: it must be before including any header files):
#define _CRT_SECURE_NO_DEPRECATE
2. Or declare #pragma warning(disable:4996)
3. Change the preprocessing definition:
Project -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definition, add:
_CRT_SECURE_NO_DEPRECATE
Method three: Method two does not use the more secure CRT function, which is obviously not a good method worth recommending, but we don’t want to change the function names one by one. Here is an easier method:
Define the following macros in the precompiled header file stdafx.h (also before including any header files):
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
When linking, it will automatically replace the old functions with Security CRT functions.
Note : Although this method uses a new function, it cannot eliminate the warning (see the red letter for the reason). You have to use method two (-_-) at the same time. In other words, the following two sentences should actually be added to the precompiled header file stdafx.h:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
Explanation of the cause of the error:
This kind of warning from Microsoft is mainly because of the functions of the C library. Many functions do not perform parameter detection (including out-of-bounds). Microsoft is worried that using these will cause memory exceptions, so it rewrites the functions of the same function. The function of has carried out parameter detection, and it is safer and more convenient to use these new functions. You don’t need to memorize these rewritten functions specifically, because the compiler will tell you the corresponding safe function when it gives a warning for each function. You can get it by checking the warning message. You can also check MSDN for details when you use it.
Read More:
- Visual Studio 2012 error C4996: ‘scanf’: This function or variable may be unsafe.
- .NETproject compilation error. Type or namespace name could not be found. Visual studio automatically introduces dependency package (shortcut key)
- visual studio Error D8016 ‘/ZI’ and ‘/Gy-‘ command-line options are incompatible
- Visual Studio SignTool Error: No certificates were found that met all the given criteria.
- [Solved] CMake Error: Could not create named generator Visual Studio 16 2019 -A Win64
- [Solved] Visual studio 2022 error LNK2019: unable to resolve external symbol for
- [Solved] CUDA fails to compile in visual studio and throws error msb3721 and nvcc fatal
- [Solved] CMake Error: Could not create named generator Visual Studio 16 2019
- Error (active) e1696 cannot open source file “errno.H” rpgworld problems encountered in installing visual studio 2017
- [Solved] Unity Error: Visual Studio Editor Package version 2.0.11 is available……..
- Visual Studio Code Error command ‘markdown.extension.onBackspaceKey’ not found
- QT Error: ‘C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\link.exe‘ failed
- Solution of adding H264 error in ffmpeg compilation
- [Solved] error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools“
- Android studio version 3.0 import version 2.2.2 error Error:This Gradle plugin requires Studio 3.0 minimum
- How to Fix “Microsoft Visual C ++ 14.0 is required” Error
- [Solved] Compilation error: dereferencing pointer to incomplete type…
- Apktool back compilation error [How to Solve]
- How to Solev QT compilation error “cannot find – LGL”
- How to Solve Nginx cross compilation Error