"C" will output the prime number between M~n and VS2005 to the C language scanf warning Warning C4996

Source: Internet
Author: User
Tags square root

A prime number is also called Prime, which is the number of natural numbers greater than 1, except 1 and the integer itself, which cannot be divisible by other natural numbers excluding 0.

It should be noted that whether a natural number n is a prime, no need to test from the 1-n can be with this number of integers, just need to use less than equal to the square root of all the prime numbers and this number to try to remove, so that the test of each number of time complexity, a full reduction of half.

The implementation code for the C language is as follows:

#include <stdio.h> #include <math.h>void main () {int m,n;printf ("The number of outputs from M to n: \ n");p rintf ("m="), scanf ("%d", &M);p rintf ("n="), scanf ("%d", &n), for (int i=m;i<=n;i++) {int flag=1;//use flag to control whether the prime number is output instead of directly pass out the prime for (int j=2;j<=sqrt (float (i)); J + +) {if ((i%j) ==0) {flag=0;break;}} if (flag) {printf ("%d,", I);}}}
The results of the operation are as follows:


If the scanf warning appears in the version above VS2005, warning C4996: ' scanf ': This function or variable can be unsafe. Consider using scanf_s instead. To disable deprecation, use _crt_secure_no_warnings. See online Help for details

In a Windows environment, it is not recommended to use the ANSI string type, and the printf () series functions have been deprecated, using a Unicode string instead of the function form followed by _s. The string that is received with the scanf () function is likely to go wrong, and ANSI is a string that ends with 0, but the second bit of some characters in the Unicode string is 0, so scanf () only receives the string of code that precedes 0. Now windows, all internally using Unicode string processing, if your program is forced to use ANSI string form, then Windows will do an extra action to convert these strings to Unicode code, in return to your program, if you use printf (), Again, back to ANSI code. This warning is given by the new C compiler, which makes sense. General Warning class information, can be ignored and forcibly compiled, but also can pass, but does not guarantee that the implementation of normal.

There is no scanf_s () in ANSI C, only scanf (), scanf () does not check boundaries when reading, so it can cause internal leaks. So scanf_s () is provided in vc++2005/2008, and when called, a number must be provided to indicate the maximum number of characters to read.

This is the IDE's security check, and the C language itself does not matter. The use of scanf_s () is as follows:

#include <stdio.h> #include <stdafx.h>int main () {int a,b,sum;printf ("Please enter two integers \ n"), scanf_s ("%d,%d", &a , 1,&b,1); sum=a+b;printf ("%d", sum); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C" will output the prime number between M~n and VS2005 to the C language scanf warning Warning C4996

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.