1. 程式人生 > >【問題】warning C4996: 'scanf': This function or variable may be unsafe

【問題】warning C4996: 'scanf': This function or variable may be unsafe

問題

經常有初學者在學習C/C++語言的時候,使用VS系列編譯器。
在使用scanf函式從標準輸入讀取字元、數字的時候,看到編譯器報一個警告:

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.

很無奈,熟手無策,這裡給出解決方案:

解決方案

在你的程式碼的最前面(一定)加上下面這句程式碼,即可解決問題,試試?

#define _CRT_SECURE_NO_WARNINGS 1

那豈不是每個原始檔都要加這句話嗎?
有麼有一勞永逸的辦法?
有!
在你VS安裝目錄下:
例如我的安裝目錄在C盤,找到下面的目錄:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcprojectitems

在這個目錄下有一個檔案叫newc++file.cpp,開啟這個檔案,在裡邊加上下面的程式碼。

#define _CRT_SECURE_NO_WARNINGS 1

以後你建立的每個原始檔中都會有這句話,這樣就再也不用擔心看到這個警告了。

但是肯定有人問,為什麼不使用警告中的scanf_s

函式,我想說的是這個函式是VS編譯器提供的,而不是C語言標準定義的,所以這個函式是不具有跨平臺的程式碼,只在VS編譯器可以被編譯使用。所以不考慮使用,而如果你的程式碼就只是在windows上使用,可以考慮。

相關推薦

問題warning C4996: 'scanf': This function or variable may be unsafe

問題 經常有初學者在學習C/C++語言的時候,使用VS系列編譯器。 在使用scanf函式從標準輸入讀取字元、數字的時候,看到編譯器報一個警告: warning C4996: 'scanf': This function or variable may be

vs的warning C4996:'fopen': This function or variable may be unsafe解決方案

二、編譯警告:warning C4996 與 Security Enhancements in the CRT 將過去的工程用VS2005開啟的時候。你有可能會遇到一大堆的警告:warning C4996。 比如:warning C4996: 'strcpy': This function or varia

VS2013使用scanf、gets及字串函式編譯報錯error C4996: 'scanf': This function or variable may be unsafe. 原因及解決方案

VS2013使用scanf、gets及字串函式編譯報錯error C4996: ‘scanf’: This function or variable may be unsafe. 原因及解決方案 問題原因: C語言的標準函式中,一些讀取或寫入記憶體的函式存在記憶

VS2015:error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead

使用VS2015學習比較傳統的C語言或者做演算法題目的時候經常會使用到scanf但是,這個函式有點玩具函式的意味,並不安全。所以目前的開發中已經非常不建議使用,至於原因,就像這位大哥說的:新版vc庫新增的警告 讓我們欣賞完這個錯誤的全部內容: error

error C4996: 'scanf': This function or variable may be unsafe.Visual Studio系列編譯器使用scanf函式報錯

在使用Visual Studio 系列編譯器使用scanf函式是,是否需到下面這句話呢?error C4996: ‘scanf’: This function or variable may be unsafe. Consider using scanf_s i

Visual Studio 2013 編譯錯誤error C4996: 'sprintf': This function or variable may be unsafe. 的解決方案

在VS 2013 中編譯 C++ 語言專案,如果使用了 sprintf函式,編譯時便會提示如下錯誤: 錯誤    5    error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s i

錯誤處理error C4996: 'fopen': This function or variable may be unsafe

編譯出錯資訊:錯誤    1    error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation,

C++程式設計:用二分法求方程解(以及解決VS 2017中'scanf':this function or variable may be unsafe.`問題)

前言 本文旨在用C++解決問題:“用二分法求方程的解。”以及解決VS 2017 中報錯問題:C4996 'scanf':this function or variable may be unsafe. 一、題目描述 給出等式8x4+7x3+2x2+3x+6

error C4996: 'sprintf': This function or variable may be unsafe.Consider using sprintf_s instead.

直接使用微軟推薦的修改後的函式sprintf_s, 其他類似的函式報錯也同樣 亦可以 選項Project   |   Configuration   Properties   |   C/C++   |   Preprocessor   |   Preprocesso

Visual Studio 2013 編譯錯誤error C4996: 'scanf': This function or variable may be unsafe.

使用vs2013的scanf函式時出現錯誤,在網上找到的解決方法,轉載過來,我使用的方法二 error C4996: ‘scanf’: This function or variable may be unsafe. Consider using scanf_s instead. To

vs2013/2015中scanf函數類似於error C4996: 'scanf': This function or variable may be unsafe的安全檢查錯誤

span 調試 ria 安全性 init 點擊 scan online pan   在使用vs2015時,遇到了scnaf函數安全性的問題,程序不能正常運行,錯誤如下: error C4996: ‘scanf‘: This function or variable may

c++ 去除 warning C4996 This function or variable may be unsafe.

原因是高版本的vs使用了更加安全的run-time library routines。 新的Security CRT functions(就是那些帶有“_s”字尾的函式) 出現這樣的警告,是因為VC2005之後的版本中認為CRT中的一組函式如果使用不當,可能會產生諸如記憶體洩露,緩衝區溢位,非法

《VS如何解決warning C4996: 'vsprintf': This function or variable may be unsafe.問題》

問題描述  使用VS2010在生成解決方案的時候,輸出視窗會出現一大堆warning C4996: 'xxx': This function or variable may be unsafe.這樣的警告資訊。參考了另外一位博主的說法:這是微軟的警告,主要是因為那些C庫的函

VS2017出現C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead.錯誤

VS2017利用fopen和fscanf讀取檔案時出現以下錯誤資訊: C4996 ‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation,

錯誤處理error C4996: 'fopen': This function or variable may be unsaf

編譯出錯資訊:錯誤    1    error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, u

解決This function or variable may be unsafe

idt 技術 erro 預處理 管理 復雜 屬性 safe nbsp 當使用VS編寫C/C++時,經常會遇到的一個錯誤是:   error C4996: ‘*****‘: This function or variable may be unsafe.   哎?可我明明使用

VS2013執行C++報錯:This function or variable may be unsafe. Consider using fopen_s instead.

在vs2013中執行時發生的關於方法呼叫的不安全錯誤。 1.更換方法,但是有些方法更改後引數不變,所以可能比較麻煩。 2.新增一條前處理器定義: 點選專案——>屬性——>c/c++——>前處理器——>預處理定義——>點選下拉按鈕——>編輯——>新增“_CRT_S

"無法解析的外部符號" 和 "This function or variable may be unsafe"

1、問題一: 無法解析的外部符號 》》》 , 一般是沒有新增庫檔案(lib)或者標頭檔案; 2、This function or variable may be unsafe 。 (1)用VS2013打開出現錯誤的程式碼檔案 (2)在工程檔名處右擊滑鼠開啟快捷選單,

OpenGL報錯:fopen:this function or variable maybe unsafe

VS2015+OpenGL+win10 64bit 編譯報錯:error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable d

解決MATLAB出現"??? Undefined function or variable 'x'."的問題,一個等號引發的大戰

fin r語 true r語言 檢查 fine 沒有 tlab 改進 最近寫了一段代碼,一直彈出“??? Undefined function or variable ‘x‘.”這個錯誤。仔細檢查了一下,發現是賦值的問題。比如下面兩段代碼 h=