Summary of issues encountered when using SCANF statements in the C language

Source: Internet
Author: User

When writing C language code using visual studio2013, there are a few minor issues that are summarized below.

1, about the solution using the SCANF statement error 1
 #include <stdio.h>int  Main (void    char      Ch printf (  please Enter a character.\n      ); scanf (  %c        , &ch); printf (  the code for%c is%d.\n  Span style= "color: #800000;" > "  return  0  ;}  

As shown above in the code, before the other compile platform, it can run normally, for example, when you enter the letter C, you will print out the code for C is. However, running in visual studio2013 will prompt the following error:

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.
As prompted, we change scanf to scanf_s, run again, and find that the compilation error is no longer being prompted. Analyze the reason carefully:

In fact, from the official website of the document can be learned that this problem, just because this is the new version of the VC library added warning, Microsoft believes that the use of scanf is a security risk, because the string processing in C + + is the cutoff character, if the search is not 0, the so-called Security standard library, which is prone to string crossing all VC extensions, adds a parameter to specify the length of the string parameter to avoid this security risk. In fact, to avoid this problem, the solution is very simple, the mouse click "Project file", press Alt+enter, display the following interface,

Click on C + +, preprocessor options, select the right side of the pre-processor definition, and select Edit and add a line in the edit box _crt_secure_no_deprecate OK. Press the CTRL+F5 running program is not error, the program is running normally,

2. Follow the system prompts to change scanf to scanf_s

If, we follow the system prompt to change to scanf_s, we run the program, the system no longer error, but the program runs, we also enter input C, see the results as shown below,

Then the question comes again, no matter what character we enter, the system output is always the code for is 0. only for what, we look at the official answer to this question (link address http://msdn.microsoft.com/en-us/library/w40768et.aspx),

A single character could be read as follows:

<P>Char  C; <p>scanf_s ("%c"1);</p>

When multiple characters for non-null terminated strings is read, integers is used as the width specification and the BU Ffer size.

<P>Char c[4]; <p>scanf_s ("%4c"// not null terminated</p>

It turns out that because scanf_s is using this feature, there are new rules that must be declared on the last side to read the length of the string, and we follow the remarks on MSDN above to change the program as follows:

#include <stdio.h>intMainvoid){    Charch; printf ("Please enter a character.\n"); scanf_s ("%c", &ch,1);//read a character from the keyboardprintf"The code for%c is%d.\n", CH, ch); return 0;}

Run the program again and the output is as follows:

Summary: Most of the time, official documentation is the most authoritative reference, and as a developer, learning to use official documents efficiently is especially important.

Summary of issues encountered when using SCANF statements in the C language

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.