9 programming habits of successful software developers 7

zhaozj2021-02-08  215

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 following user input data: char city [10];

/ * Arrangement for urban 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 do an adventure, you have to check the length of the text:

CHAR City [10];

/ * Arrangement for 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, and it will not be accepted.

9. Never believe users

This is 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.

Whele is incorrectly chopping

The above program habits 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-3264.html

New Post(0)