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

Source: Internet
Author: User

Reprint: http://www.th7.cn/Program/c/201303/127343.shtml

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 declaration # #pragma 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 inside is not for parameter detection (including out-of-bounds classes), Microsoft is concerned about the use of these can cause memory anomalies, Therefore, the function of the same function is rewritten, the rewritten function is used to detect the parameters, it is more safe and convenient to use these new functions. 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

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

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.