Go Visual Studio 2012 Compilation Error "Error C4996: ' scanf ': This function or variable could be unsafe. "Solution

Source: Internet
Author: User

Original address: http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html

Compiling the C language project in vs 2012, if you use the SCANF function, you will be prompted with the following error when compiling:

Error C4996: ' scanf ': This function or variable could 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 a more secure Run-time library routines. The new security CRT functions (which are those with the "_s" suffix), see:

Security-enhanced versions of CRT functions

Here's a solution to this problem:

Method One: Replace the original old function with the new Security CRT functions.

Method Two: Block this warning in the following ways:

1. In the precompiled header file StdAfx.h (Note: Be sure to define the following macro before you do not include any header files):

#define _crt_secure_no_deprecate

2. Or statement #param Warning (disable:4996)

3. Change the preprocessing definition:

Property---Properties---config attribute---preprocessor---preprocessor definition, add:

_crt_secure_no_deprecate

Method Three: Method two does not use a more secure CRT function, obviously not a good way to recommend, but we do not want to change the function name one by one, here is a more convenient way:

Define the following macro in the precompiled header file StdAfx.h (also before you include any header files):

#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

The old function is automatically replaced with the Security CRT functions at the time of the link.

Note : Although this method uses a new function, but does not eliminate the warning (for the reason of the red word), you also have to use method two (-_-). That is, you should actually add the following two sentences 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 Microsoft warning, mainly because of those C library functions, many functions are not for parameter detection (including out-of-bounds classes), Microsoft is concerned about the use of these will cause memory anomalies, so rewrite the same function, the rewritten function of the parameters of the detection, the use of these new functions will be more secure and convenient. You don't have to memorize these rewritten functions, because the compiler will tell you the appropriate security function for each function when it gives a warning, see the warning message, and look at MSDN for more information when you use it.

Reference: security Template overloading

Go Visual Studio 2012 Compilation Error "Error C4996: ' scanf ': This function or variable could be unsafe. "Solution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.