当前位置:网站首页>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中。。
边栏推荐
- 实例006:斐波那契数列
- Negative pressure generation of buck-boost circuit
- STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
- Talk about the circuit use of TVs tube
- Anonymous structure in C language
- On boost circuit
- DokuWiki deployment notes
- Five design details of linear regulator
- STM32 --- NVIC interrupt
- 动力电池UL2580测试项目包括哪些
猜你喜欢
AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)](/img/3b/c94b8466370f4461875c85b4f66860.png)
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)

Ble encryption details

实例008:九九乘法表

Classic application of MOS transistor circuit design (2) - switch circuit design

Explain task scheduling based on Cortex-M3 in detail (Part 2)

List of linked lists

猜谜语啦(4)

【NOI模拟赛】汁树(树形DP)

Several important parameters of LDO circuit design and type selection

实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
随机推荐
Synchronization of QT multithreading
猜谜语啦(5)
STM32---ADC
Cinq détails de conception du régulateur de tension linéaire
Infected Tree(树形dp)
STM32 single chip microcomputer -- volatile keyword
Weidongshan Internet of things learning lesson 1
2022.7.4-----leetcode.1200
Briefly talk about the identification protocol of mobile port -bc1.2
STM32 tutorial triple ADC interleaved sampling
猜谜语啦(142)
剑指 Offer 05. 替换空格
STM32 --- GPIO configuration & GPIO related library functions
Stm32--- systick timer
Array integration initialization (C language)
Arduino burning program and Arduino burning bootloader
One question per day - replace spaces
Esphone Feixun DC1 soft change access homeassstant
猜谜语啦(9)
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)