在Vsual Stdio 2019調用scanf函數出現安全警告
解決方法
-
- 方法1
- 方法2:
- 方法3:
方法1
在檔案頂部添加代碼:
#pragma warning(disable:4996)
或者
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> //#include<iostream>
//using namespace std;
int main()
{
int h, f, x, y;
printf("請輸入雞和兔的總頭數:");//cout<<"請輸入雞和兔的總頭數: "<<endl;
scanf("%d", &h);//cin>>h;
printf("請輸入雞和兔的總腳數:");//cout<<"請輸入雞和兔的總腳數:"<<endl;
scanf("%d", &f);//cin>>f;
x = (4 * h - f) / 2;
y = (f - 2 * h) / 2;
printf("雞的頭數為%d,兔的頭數為%d", x,y);//cout<<"雞的頭數為"<<x<<",兔的頭數為"<<x<<"頭"<<endl;
return 0;
}
方法2:
右擊工程中源代碼(main.c)處,打開屬性欄>配置屬性>C/C++>所有選項,将SDL檢查改為否,儲存
方法3:
将
scanf
改為
scanf_s
scanf等類似的函數已經不太安全,要想保證程式的安全性,建議以後采用_s結尾的安全版本,但是很多以前的程式可能還是使用不安全的版本。
整這個最有效,沒有任何錯誤,完全正确!
#include<stdio.h>
int main()
{
int h, f, x, y;
printf("請輸入雞和兔的總頭數:");
scanf_s("%d", &h);
printf("請輸入雞和兔的總腳數:");
scanf_s("%d", &f);
x = (4 * h - f) / 2;
y = (f - 2 * h) / 2;
printf("雞的頭數為%d,兔的頭數為%d", x,y);
return 0;
}