当前位置:网站首页>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中。。
边栏推荐
- Is the security account given by Yixue school safe? Where can I open an account
- 287. 寻找重复数-快慢指针
- DokuWiki deployment notes
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- 剑指 Offer 05. 替换空格
- 实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- STM32 single chip microcomputer -- volatile keyword
猜你喜欢
List of linked lists
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
STM32 lights up the 1.8-inch screen under Arduino IDE
Old Wang's esp8266 and old Wu's ws2818 light strip
Sword finger offer 05 Replace spaces
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
Explore the authentication mechanism of StarUML
OC and OD gate circuit
How to write cover letter?
STM32 --- NVIC interrupt
随机推荐
猜谜语啦(5)
Go dependency injection -- Google open source library wire
Working principle and type selection of common mode inductor
QEMU demo makefile analysis
Arduino+a4988 control stepper motor
剑指 Offer 09. 用两个栈实现队列
动力电池UL2580测试项目包括哪些
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Take you to understand the working principle of lithium battery protection board
实例008:九九乘法表
DCDC circuit - function of bootstrap capacitor
猜谜语啦(7)
Anonymous structure in C language
319. 灯泡开关
Count the number of inputs (C language)
Negative pressure generation of buck-boost circuit
MATLAB小技巧(28)模糊综合评价
FIO测试硬盘性能参数和实例详细总结(附源码)
Five design details of linear regulator
Detailed summary of FIO test hard disk performance parameters and examples (with source code)