当前位置:网站首页>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中。。
边栏推荐
- Working principle and type selection of common mode inductor
- Low code platform | apaas platform construction analysis
- What are the test items of power battery ul2580
- 2022.7.4-----leetcode.1200
- Circleq of linked list
- leetcode - 445. 两数相加 II
- FIO测试硬盘性能参数和实例详细总结(附源码)
- L298N module use
- go依赖注入--google开源库wire
- Take you to understand the working principle of lithium battery protection board
猜你喜欢
随机推荐
Example 009: pause output for one second
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Esphone retrofits old fans
2022.7.4-----leetcode.1200
Stm32--- systick timer
猜谜语啦(3)
Shell script realizes the reading of serial port and the parsing of message
NTC thermistor application - temperature measurement
猜谜语啦(6)
Cinq détails de conception du régulateur de tension linéaire
实例006:斐波那契数列
Anonymous structure in C language
动力电池UL2580测试项目包括哪些
Zero length array in GNU C
Synchronization of QT multithreading
Arduino+a4988 control stepper motor
猜谜语啦(10)
One question per day - replace spaces
2020-05-21
Installation and use of libjpeg and ligpng
![[noi simulation] juice tree (tree DP)](/img/19/bc71e8dc3958e4cb87b31423a74617.png)







