当前位置:网站首页>C string input considerations
C string input considerations
2022-06-24 03:51:00 【Nine questions】
String input function
Here are some common string input functions , And their use of the relevant deficiencies
## scanf() function
Beginners learn C Language , The first string input function encountered may be scanf 了 , Stop typing when we knock on the car . However , It also has some flaws , And these defects are sometimes very deadly . For example, when we type scanf("%5s %10s",str1,str2);, And then print it printf("the string1 is %4s and the string2 is %5s",str1,str2) among str1,str2 It's a length of 10 Of char Type array , When we enter a value of **zifuchuan shuru**, Will it print out **zifuchuan shuru** Well ? The answer is no , This is also used scanf() Functions should pay attention to , When printing, because the conversion format is %4s, So it just reads in **zifu**, And then meet %5s After the **chuan** Read in . hinder **shuru** If not cleared , It will read... On the next call , In this way, we will not be able to read the string we want to use the next time , If the next input is not a string, it may even cause a program error . then scanf Another drawback is that only one word can be read , Can't read sentences with spaces .
## gets() function
gets() The function is simple and easy to use , For example, the following code
char words[100]; gets(words);
Let's type in a paragraph like **you are a good boy**, It will read all this paragraph in , Including Spaces , Stop typing until a newline character is encountered , Then add... At the end '\n' The empty character makes this passage a string .gets() Easy to use at the same time , It also has serious defects , Take the above code as an example ,words The length of the array is limited to 100 Elements , Each element is char Type of characters , The last element is a null character , So actually we can only enter 99 Characters , When we enter more than characters 99 when , That is to say, there is no reserved space for extra characters , The consequence is buffer overflow, If only the memory space occupied for use is OK , If it takes up the space of other processes , This will cause the program to break abnormally .gets() We won't be reminded when we enter extra characters . therefore C11 Removed from the standard gets() function , Of course, in order to be compatible with the previous code , Most compilers continue to support .
## fgets() function
fgets() The first parameter is char str, That's the pointer , Point to chat type , It is usually the address where the string is stored , The second parameter is the maximum number of read characters , The third is FILE stream, Indicate the file to be read in , Usually when we read from the keyboard , With stdin As a parameter . because fgets() Limited number of characters , So it's avoided buffer overflow, in addition ,fgets() If the input does not overflow, the newline character will also be stored , So cooperate with fputs(), It will automatically wrap .fgets() The function returns a pointer to char The pointer to , The address returned during successful reading is the same as the first one , If you reach the end of the file , Will return null pointer,
Common mistakes
No space allocated for string
For the following code ,
char *name;
printf("%s\n",name);So what is the problem with this code ? We defined the point char Pointer to type , Then it does not initialize ,name What is stored is the address to which it points ? He may just store it casually , And memory does not allocate space for it .
边栏推荐
- 元气森林推“有矿”,农夫山泉们跟着“卷”?
- Discussion on the introduction principle and practice of fuzzy testing
- golang clean a slice
- How to choose excellent server hosting or server leasing in Beijing
- Rasa 3. X learning series -rasa 3.2.0 new release
- Grpc: how to make grpc provide swagger UI?
- 一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
- Received status code 502 from server: Bad Gateway
- How EDI changes supply chain management
- [Numpy] Numpy对于NaN值的判断
猜你喜欢

【代码随想录-动态规划】T392.判断子序列

元气森林推“有矿”,农夫山泉们跟着“卷”?
![[numpy] numpy's judgment on Nan value](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)
[numpy] numpy's judgment on Nan value

Installation of pytorch in pycharm

黑帽SEO实战搜索引擎快照劫持

Old popup explorer Exe has stopped working due to problems. What should I do?
![[code Capriccio - dynamic planning] t392 Judgement subsequence](/img/59/9da6d70195ce64b70ada8687a07488.png)
[code Capriccio - dynamic planning] t392 Judgement subsequence

黑帽实战SEO之永不被发现的劫持

618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?

Halcon knowledge: contour operator on region (2)
随机推荐
ClickHouse(02)ClickHouse架构设计介绍概述与ClickHouse数据分片设计
hprofStringCache
Some basic knowledge of data center server cabinet
Optimization of digital transformation management of procurement platform in construction industry
Summary of rust high concurrency programming
[competition experience sharing] design of intelligent guide rod
Black hat SEO actual combat search engine snapshot hijacking
RPM 包的构建 - SPEC 基础知识
Browser rendering mechanism
Summary of common problems of real-time audio and video TRTC - quality
How do websites use CDN? What are the benefits of using it?
Differences between EDI and VMI
Received status code 502 from server: Bad Gateway
Rasa 3. X learning series -rasa 3.2.0 new release
Black hat SEO actual combat directory wheel chain generates millions of pages in batch
Several key tools for cloud native implementation
13. Tencent cloud IOT device side learning - data template function and Implementation
Web penetration test - 5. Brute force cracking vulnerability - (2) SNMP password cracking
Spirit breath development log (17)
Old popup explorer Exe has stopped working due to problems. What should I do?