当前位置:网站首页>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中。。
边栏推荐
- Cmder of win artifact
- STM32---IIC
- Sizeof (function name) =?
- [nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
- 關於線性穩壓器的五個設計細節
- 2022.7.4-----leetcode. one thousand and two hundred
- MATLAB skills (28) Fuzzy Comprehensive Evaluation
- 【三层架构】
- Naming rules for FreeRTOS
- STM32 outputs 1PPS with adjustable phase
猜你喜欢

Talk about the function of magnetic beads in circuits

Management and use of DokuWiki (supplementary)

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

Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?

Shell script

Several important parameters of LDO circuit design and type selection

Low code platform | apaas platform construction analysis

Keil use details -- magic wand

Arduino+a4988 control stepper motor

Detailed summary of FIO test hard disk performance parameters and examples (with source code)
随机推荐
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
On boost circuit
Classic application of MOS transistor circuit design (2) - switch circuit design
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Esphone retrofits old fans
Keil use details -- magic wand
Use indent to format code
Low code platform | apaas platform construction analysis
【三层架构及JDBC总结】
Cinq détails de conception du régulateur de tension linéaire
猜谜语啦(3)
Explore the authentication mechanism of StarUML
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
剑指 Offer 06. 从尾到头打印链表
Matlab tips (28) fuzzy comprehensive evaluation
MySQL MHA high availability cluster
Chapter 18 using work queue manager (1)
Management and use of DokuWiki
STM32 outputs 1PPS with adjustable phase
Meizu Bluetooth remote control temperature and humidity access homeassistant