Top 10 points for writing C Programs

Source: Internet
Author: User

Top 10 points for writing C Programs

To write a C Language Program, the most important thing is to clearly analyze the problems to be solved, design a solution to the problem, and implement the process of solving the problem through computation, perform a scientific and structured decomposition of the problem solving process. The following suggestions may be helpful when we further consider how to write a program.

Some of these suggestions are general, not only effective for the C language program design, but also some are especially for the C language program design. This table will be further modified and expanded. Comments are welcome.

 

1) Pay special attention to the writing format of the program, so that its form reflects its internal meaning structure.

Programs are the most complex (although the programs you start to write are simple, they gradually become complicated) and are intellectual products that need intelligence to grasp. A good format can make the program structure clear at a glance, help you and others understand it, help your thinking, and help you find abnormal places in the program, so that errors in the program can be easily discovered.

The common format is: Logical alignment of the same layer; logical alignment of the internal layer is pushed to the next alignment position. Please refer to the textbooks of this course or the C programming language (the C programming language, Brian W. kernighan & Dennis M. rirchie, Tsinghua University Press, university computer education series (photocopy, English), 1996 .)

By using the integrated development environment (IDE) or other program editor functions, you can easily maintain a good program format. Note the following keys, which should be frequently used in the Write Program: enter (for another line ), tab key (move the input cursor to the next alignment position-enter a new level) and backspace key (return to the previous alignment position-return to a level outside ).

 

2) write a program in the most standard, clear, and understandable way. Note that people are familiar with writing programs in C language, such as the way they solve similar problems in textbooks. There are many excellent program examples in C programming language. There is a webpage on the program mode, which also lists some common modes.

C language is a very flexible language. You may write programs in many obscure ways here, but such a program can only be used as a theme, like a riddle or an intellectual game. These things can be used to consume time, but they are usually not practical. We mentioned this in our C language discussion group.

 

3) in the compilation process, the error information and warning information provided by the compilation program should be carefully studied to find out the exact root cause of each information and solve it. In particular, do not ignore the warning information. Many warning messages are derived from hidden serious errors. There are many ways to fool the compilation program so that it cannot discover errors in our program, but in the end, we can only be hurt by ourselves.

 

4) Pay attention to the expression calculation process and type at any time. Pay attention to the priority and order of the operators, how the operation objects of different types are converted, and what type of operation results the operation results are. Add parentheses or explicit type forced conversions when necessary.

There are many operators in the C language, and the priority definition is not reasonable, so it is difficult to fully remember, so pay special attention to it. If necessary, check (do not worry, there is an operator table on the relevant webpage), or add several parentheses as needed.

 

5) do not write expressions that depend on the order in which the computing objects are evaluated. For calculation objects of common binary operators, the C language does not specify the specific order of value for each actual parameter of function calls. Therefore, we should not write the expression that depends on the specific order of evaluation, because it cannot guarantee what results it will get. For example, the following expressions and function calls are both inappropriate and may produce unexpected results:

Scanf ("% d", I ++, a [I]);
M = N * n ++;

 

6) always ensure that a function's definition point and all its usage points are visible.A complete function prototype description. Refer to page 103-107 of problem-to-procedure.

 

7) always check the bounds of the array and the end of the string (also stored as an array. The C language does not check whether the value of the array subscript expression is within the valid range, nor whether the pointer to the array element is removed from the legal area of the array. The program writer needs to ensure the validity of array use. Cross-border access may cause disastrous consequences.

For example, when writing a function to process an array, there should be a range parameter. when processing a string, always check whether there is a null character '/0 '.

 

8) No indirect access to null or hanging pointers is allowed. The consequences of such access are unpredictable, which may cause damage to the system, or the operating system may find that the program performs illegal operations and forcibly terminates it.

 

9) for all database functions that report the running status or error information through the return value, check whether the execution is normal. If the library function is not completed (for various reasons), subsequent operations may be invalid. This kind of error may also be hidden for a long time in the program running, and it will only be exposed later. It is very difficult to check the error.

 

10) in a defined string with a macro parameter, brackets should be placed for the entire string and each parameter that appears.

C language preprocessing is a simple text replacement program, which does not know the syntax structure and priority rules of C language. If we do not write parentheses, we sometimes do not want replacement results.

 

11) all external variable names and function names should be distinguished by only the first six characters. Because some old compilation programs only focus on the first six characters of these names. If you do not pay attention to this problem, it may cause an implicit connection error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.