当前位置:网站首页>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 .

原网站

版权声明
本文为[Nine questions]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210917190323875H.html