9 programming habits of successful software developers (7)

zhaozj2021-02-17  51

8. Confirm that the buffer wants to confirm the size of the set alignment or variable to avoid data chaos or cause the system. Please see the user input data: char city [10]; / * is the metropolitan name * / printf ("Enter a city name:"); scanf ("% s", city); printf ("city IS% S / N ", city; here, the urban name is set to 10 words (English characters). What if the user enters the urban name of 10 words or more? It can be said that the program fails or covers the data in the buffer. Anyway, don't take a risky thing, you have to check the length of the text: CHAR City [10]; / * is the arrangement of urban name * / Printf ("Enter a city name:"); Fgets (City, Sizeof (City) , stdin; Printf ("City IS% S / N", City; The above has been modified is: If the user enters the urban name of 10 words, only the length of the buffer can accept, the outside will not accept. 9. It is absolutely not believed that users are very important. Don't believe that people who will use your software, don't think that users will operate according to your hopes, but they want them to discover programs. For example, the above program is to confirm the length of the text for those users who enter the urban name. As the C language requires a clear data form, it is necessary to remember to confirm whether the input data form and the program data form are consistent, or there will be problems. The program habits of the sharpener incorrect slack work are for your own convenience. Before actually programming, prepare more (analysis, design). Although it is slightly trouble, but in order to compose a neat program, don't give it time to prepare, otherwise you will need more time to modify, expand your program.

转载请注明原文地址:https://www.9cbs.com/read-29465.html

New Post(0)