当前位置:网站首页>scanf返回值被忽略的原因及其解决方法
scanf返回值被忽略的原因及其解决方法
2022-06-12 10:44:00 【m0_61083409】
昨天在使用Visual Studio 2019编写C语言程序时遇到了scanf返回值被忽略问题

因为我也是刚开始使用VS2019学习C语言,第一次遇到这种问题,也不知道怎么回事,然后就上Chrome研究了一番,才知道原因,并且找到了几种分散在个个角落的解决方法,我在这里归纳总结一下。
问题原因:
在ANSI C中只有scanf(),没有scanf_s(),但是scanf()在读取时不检查边界,所以可能会造成内存泄漏。于是Microsoft公司在VS编译器中提供了scanf_s(),如果想继续使用scanf这个不安全的函数,可以通过以下方法解决。
方法①:
把scanf改成:scanf_s
这是VS编译器特有,既然觉得scanf()不安全,那它就自己定义了一个scanf_s()。

方法②:
编译器错误提示的原因至于VS中的SDL检查,只需要找到它并关闭就可以了。
关闭步骤:右键“项目文件” —— “属性” —— “配置属性” ——“C/C++”——“常规”——“SDL检查”改为“否”即可,如图所示
改为否后,再次编译就没问题了。
方法③:
在C语言程序开头添加代码:#pragma warning(disable:4996)
//全部关掉
(或者)
在C语言程序开头添加代码:#pragma warning(once:4996)
//仅显示一个

方法④:
在C语言程序开头添加代码:#define _CRT_SECURE_NO_WARNINGS
//忽略安全检测
每次都要加这么一串代码的话是不是感觉有点费力??(我一开始找到这种方法的时候第一感觉也是这样的)
其实不用那么麻烦,我们只需要在VS中加入 _CRT_SECURE_NO_WARNINGS 这个宏就可以了,具体操作如下图所示:
一次搞定,后面都不用再弄了。我个人偏向于第④种方法。
边栏推荐
- Solution to the problem that the applet developer tool cannot input simplified Chinese
- 淺談調和形狀上下文特征HSC對3DSC的改進
- Common tools download address
- How to play the 618 super cat games on Taobao? Here comes the introduction to the overall activities of the Super Cat Games
- Principle analysis of vite HMR
- PHP: seven cattle cloud upload file
- Summary method of lamp environment deployment
- 元宇宙链游与传统游戏的区别
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- PHP: Excel to get the letter header
猜你喜欢

Tp6+memcached configuration

^34作用域面试题

Vite Basics

Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution

2022京东618预售定金怎么退?京东618定金能退吗?

基于QT的旅行查询与模拟系统

M-Arch(番外11)GD32L233评测-PWM驱动有源蜂鸣器

AcWing 132. 小组队列(队列模拟题)

Bug easily ignored by recursion

Telecommuting with cpolar (2)
随机推荐
tensorflow 2.x 多分类混淆矩阵及评价指标计算方法(精确率、召回率、f1分数)
AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)
How to play the 2022 Taobao 618 Super Cat Games? Playing skills of 2022 Taobao 618 Cat Games
Get array median
Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis
file_ get_ Contents() JSON after reading_ Decode cannot be converted to array
890. 查找和替换模式
Love and hate in the Jianghu
AcWing 131. 直方图中最大的矩形(单调栈经典运用 模板)
2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
This and final keywords
PHP generate schedule
PHP wechat payment V3 interface
元宇宙链游与传统游戏的区别
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time
JS string combination
PHP interface generates cache and MD5 encryption uniformly
Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
M-arch (fanwai 12) gd32l233 evaluation -cau encryption and decryption (tease Xiaobian)