Visual Studio中scanf的警告和sqrt()报错情况的处理方法

描述 现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和。
输入
第一行给出整数M(0<M<10)代表多少组测试数据
每组测试数据第一行给你N,代表该组测试数据的数量。
接下来的N个数为要测试的数据,每个数小于1000
输出
每组测试数据结果占一行,输出给出的测试数据的所有素数和
样例输入
3
5
1 2 3 4 5
8
11 12 13 14 15 16 17 18
10
21 22 23 24 25 26 27 28 29 30
样例输出
10
41
52



程序:

#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,s,M,N,a[1000];
    float k;
    scanf("%d",&M);
    while(M--)
    {
        scanf("%d",&N);
        for(i=s=0;i<N;i++)
        {
            scanf("%d",&a[i]);
            k=sqrt(a[i]);
            for(j=2;j<=k;j++){
            if(a[i]%j==0) break;
}
            
            if(!(j<=k||a[i]==1||a[i]==0))
            s+=a[i];
        }
        printf("%d\n",s);
    }
    return 0;
}


这个题目本身不难,以上程序也可以通过,在dev中可以通过,但有趣的是在Visual Studio中,会出现报错与警告,这是dev中所没有出现的,以下就是我的vs2008所显示的


1>------ 已启动生成: 项目: 求圆的面积, 配置: Debug Win32 ------

1>正在编译...
1>圆的面积.cpp
1>d:\documents\visual studio 2008\projects\project1\求圆的面积\圆的面积.cpp(7) : warning 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.
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : 参见“scanf”的声明
1>d:\documents\visual studio 2008\projects\project1\求圆的面积\圆的面积.cpp(10) : warning 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.
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : 参见“scanf”的声明
1>d:\documents\visual studio 2008\projects\project1\求圆的面积\圆的面积.cpp(13) : warning 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.
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : 参见“scanf”的声明
1>d:\documents\visual studio 2008\projects\project1\求圆的面积\圆的面积.cpp(14) : error C2668: “sqrt”: 对重载函数的调用不明确
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(581): 可能是“long double sqrt(long double)”
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(533): 或       “float sqrt(float)”
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(128): 或       “double sqrt(double)”
1>        试图匹配参数列表“(int)”时
1>生成日志保存在“file://d:\Documents\Visual Studio 2008\Projects\Project1\求圆的面积\Debug\BuildLog.htm”
1>求圆的面积 - 1 个错误,3 个警告

========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


很明显的是问题出现在scanf与sqrt()的地方,对于vs中的输入,将scanf换为scanf_s就没有这个问题,printf却没有此变化,而对于sqrt(),将sqrt(n)中乘以1.0,即sqrt(n*1.0);很明显vs比dev更加严格,这也是100M与4G的区别,总的来说一个良好的书写习惯是挺重要的

猜你喜欢

转载自blog.csdn.net/m0_37687058/article/details/59750408