Coding habits of C Programs

Source: Internet
Author: User
Tags uppercase letter
1. Write c in a concise and clear way Program . This programming method is usually called kis ("Keep concise "). Do not write programs in odd ways.

2. Read all the C version manuals. I often refer to the C version manual to understand the rich features of this version and use these features correctly.

3. Computers and compilers are good instructors. If you are unsure about a feature of C, write a simple program, compile and run it, and see what happens.

4. Add comments describing the function purpose before each function.

5. The last character printed by the function executing the print operation should be the newline character (\ n ). This ensures that the cursor is positioned at the beginning of the new line. This can improve the reusability of software (software reusability is an important goal of the software development environment ).

6. The function body defined in curly braces should be written in indent format. This method highlights the functional structure of the program and makes the program easy to read.
 
7. Establish the indentation distance you like and use it in the future. Although you can use the tab key to set the indentation distance, the stop position of the tab key may be different in different environments. It is recommended that the indention distance of each layer be 1/4 inch Tab key hop length or three spaces.

8. Although <stdio. h> is dispensable in a program, it is recommended that <stdio. h> be included in every C program that uses the standard input/output library function. This helps the compiler locate errors in the program in the compilation phase rather than the execution phase. Errors During execution can be corrected at a higher cost.

9. Add a space after each comma to improve the readability of the program.

10. selecting meaningful variable names is helpful for the program's self-contained documentation (you can use less comments ).

11. It is only used as the identifier of the variable name. The first letter must be in lower case. The identifiers starting with uppercase letters and all uppercase letters have special meanings.

12. Variable names use multiple words to make the program more readable. Do not write words together (for example, totalcommissions), but separate words with underscores (for example, total_commissions). If you really want to write words, each word after the first word starts with an uppercase letter (for example, totalcommissions ).

13. Separate declared statements and executable statements in the function with empty rows. In this way, the end of the statement Declaration and the start of the executable statement can be highlighted.

14. Set a space on both sides of the binary operator. In this way, the binary operator is eye-catching and the program can be more readable.

15. If struct is written in indent format.

16. To improve program readability, add a blank line before and after each control structure.

17. Do not write multiple statements on the same line of the program.

18. Long statements can be folded into multiple rows. When a statement must be broken, select a meaningful breakpoint (for example, after a comma is used to separate the list) and write the broken line in indent format.

19. When writing expressions containing many operators, refer to the operator priority table. Make sure that the operators in the expressions are executed in the correct order. If you are unsure of the order in which operators are computed in a complex expression, use parentheses to forcibly calculate the order. Note that some operators in the C language (such as the value assignment operator =) are combined from right to left, rather than from left to right.

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.