Tag Archives: error C4996: ‘scanf’: This function or variable may be unsafe.

Solve the security check error similar to error c4996: ‘scanf’: this function or variable may be unsafe in vs2019

The following solutions are generally used to solve this problem:

 

(1) Scanf and other similar functions are not safe. To ensure the security of the program, it is recommended to use them in the future_ S, but many previous programs may still use unsafe versions. Here are some ways to get rid of this error prompt.

 

(2) When creating a new project in VS, remove the “safe development life cycle (SDL) check” to turn the error into a warning, so that the use of unsafe versions will not affect the compilation and operation

 

(3) In the front of the header file, remember to add: #define at the front (in front of include)_ CRT_ SECURE_ NO_ WARNINGS     This macro definition

 

(4) Add: #pragma warning (disable: 4996) at the beginning of the compiled header file    

 

(5) Project properties – & gt; Configuration Properties-> C/C++-> Preprocessor-> Enter preprocessor definitions, click the button (…) and enter:_ CRT_ SECURE_ NO_ Warnings, use “\ n” to separate them.

 

In short, if you want to do no security check at all, use the method in (2) or the method in (5). If you just don’t use some files, you can use the methods in (3) and (4). If you like to use the method in (2), you can choose according to your needs. However, with the gradual improvement of the importance of security, I suggest you use the safe version in the future

————————————————
Copyright notice: This is the original article of CSDN blogger “Junlin, Tianxia”, which follows the CC 4.0 by-sa copyright agreement. For reprint, please attach the original source link and this notice
original link: https://blog.csdn.net/dan15188387481/article/details/49622783/

[Solved] VS error C4996: ‘scanf’: This function or variable may be unsafe.

Compile the C language project in VS, 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++ uses more secure run-time library routines. The new Security CRT functions (that is, those with the “_s” suffix), please 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
  1. Or statement
#pragma warning(disable:4996)
  1. 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. 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.