当前位置:网站首页>C语言标准函数scanf不安全的原因
C语言标准函数scanf不安全的原因
2022-07-05 08:32:00 【明月清风-精进不止】
scanf是标准C语言的标准库函数;
scanf_s是微软的,比scanf增加了一个参数,必须说明要输入的字符的个数;
eg:
char buf[5]={0};
scanf("%s",buf);//如果输入1234567890,则5后面的字符也会读入,可能会写入到其他变量的位置,导致程序
//异常,在VS2019下测试,程序崩溃. 如果输入12,则正常
char buf[5]={0};
scanf_s("%s",buf,4);//只能存储空间字符个数-1个字符 因为最后一个空间要存储\0
//在vs2019下测试,如果输入≤4个字符,一切正常,如果输入5个字符或者以上,程序不崩溃,但是buf中没有数据,原因应该是4个字符以内(含)没有接收到回车,所以就没写入到buf中。。
边栏推荐
- 每日一题——替换空格
- STM32 --- GPIO configuration & GPIO related library functions
- 【三层架构】
- Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
- Stm32--- systick timer
- Explore the authentication mechanism of StarUML
- Sizeof (function name) =?
- Negative pressure generation of buck-boost circuit
- MATLAB小技巧(28)模糊綜合評價
- MySQL之MHA高可用集群
猜你喜欢

Array integration initialization (C language)

猜谜语啦(9)

MySQL之MHA高可用集群

Briefly talk about the identification protocol of mobile port -bc1.2

Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off

MySQL之MHA高可用集群

Simple design description of MIC circuit of ECM mobile phone

STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)

MATLAB小技巧(28)模糊綜合評價

UE像素流,来颗“减肥药”吧!
随机推荐
如何写Cover Letter?
Summary of SIM card circuit knowledge
How apaas is applied in different organizational structures
STM32 single chip microcomputer -- volatile keyword
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
2022.7.4-----leetcode.1200
go依赖注入--google开源库wire
STM32 --- configuration of external interrupt
Keil use details -- magic wand
Five design details of linear regulator
QEMU demo makefile analysis
Problem solving: interpreter error: no file or directory
leetcode - 445. 两数相加 II
Xrosstools tool installation for X-Series
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
剑指 Offer 06. 从尾到头打印链表
Talk about the function of magnetic beads in circuits
Arduino+a4988 control stepper motor
STM32---IIC
Example 008: 99 multiplication table