当前位置:网站首页>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 这个宏就可以了,具体操作如下图所示:
一次搞定,后面都不用再弄了。我个人偏向于第④种方法。
边栏推荐
- MySQL injection load_ File common path
- 浅谈三维形状上下文特征3DSC理论及应用
- [experiment] MySQL master-slave replication and read-write separation
- PHP uses leanclound to save associated properties
- Sendmail Dovecot 邮件服务器
- Several solutions across domains
- Pseudo static setting of access database in win2008 R2 iis7.5
- AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
- Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis
- MYSQL——内置函数
猜你喜欢

Malicious code analysis practice - use IDA pro to analyze lab05-01 dll

2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧

Malicious code analysis practice - lab03-02 DLL analysis

M-Arch(番外12)GD32L233评测-CAU加解密(捉弄下小编)

2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills

M-arch (fanwai 11) gd32l233 evaluation PWM driven active buzzer

Fiddler automatically saves the result of the specified request to a file

Leetcode 2169. Get operands of 0

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

Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
随机推荐
A hundred secrets and a few secrets - Caesar encryption
M-arch (fanwai 13) gd32l233 evaluation - some music
PHP wechat payment V3 interface
Distributed storage exploration
M-Arch(番外13)GD32L233评测-来点音乐
Tp6+memcached configuration
PHP can load different new data methods for each refresh
Solution to invalid small program scroll into view
Fiddler automatically saves the result of the specified request to a file
k53.第二章 基于二进制包安装kubernetes v1.22 --集群部署
ID obfuscation
分布式存储探索
2022京東618預售定金怎麼退?京東618定金能退嗎?
AcWing 41. Stack containing min function (monotone stack)
k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署
PHP interface generates cache and MD5 encryption uniformly
Machine learning is not something you can use if you want to use it
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
2021-03-24
Leetcode2154. 将找到的值乘以 2(二分查找)