Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the programming specifications of C language?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the C language programming specifications, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Basic rules

Clear format, concise comments, easy to understand naming norms, function modularization, easy to read and maintain the program, accurate implementation of functions, high code space efficiency and time efficiency, moderate expansibility, single chip microcomputer programming specification-identifier naming.

2. Identifier naming

2.1 basic principles of naming

(1) the naming is clear and has a clear meaning, using complete words or conventional abbreviations. In general, shorter words can be abbreviated by removing vowels; longer words can be abbreviated by the first few letters of the word. That is, "know the meaning by seeing the name".

(2) the naming style should be consistent from beginning to end.

(3) if special conventions or abbreviations are used in naming, there should be comments.

(4) the identifier name of the interface part between modules within the same software product is preceded by the module identification.

2.2 Macro and constant naming

Macros and constants are named in all uppercase letters, and words are separated by underscores. Apply meaningful enumerations or macros to the numbers used in the program instead.

2.3 variable naming

Variable names are named in lowercase letters, with the first letter of each word capitalized. Type prefix (U8\ S8 etc.) the global variable is prefixed with g _.

Local variables should be concise and to the point Local loop body control variables give priority to I, j, k, etc.; local length variables give priority to len, num, etc.; temporary intermediate variables give priority to temp, tmp, etc.

2.4 function naming

The function name is named in lowercase letters, the first letter of each word is capitalized, and the module identity is added first.

2.5 File naming

A file contains all the functions of a class of functions or a module, and the file name should clearly indicate its function or nature. Each .c file should have an .h file with the same name as the header file.

3. Comment

3.1 basic principles of annotations

It is helpful to read and understand the program, explain what the program is doing, and explain the purpose, function and method of the code. In general, the effective annotation amount of the source program is about 30%. The annotation language must be accurate, easy to understand and concise. Write code while commenting, modify the code and modify the corresponding comments at the same time, comments that are no longer useful should be deleted. "/ /" is used in both assembly and C #, cancel ";" do not use segment comments "/ * /" (available for debugging)

3.2 File comment

The file comment must indicate the file name, function function, creator, creation date, version information and other related information. When modifying the file code, the date and personnel of the modification should be recorded in the comments of the file, and the purpose of the modification should be briefly stated. All modification records must be kept intact. The file note is released at the top of the file and is included in the "/ *. * /" format. Comment text is indented by 4 spaces per line; the section name of each comment text should be aligned.

/ *

File name:

Author:

Version:

Description:

Modify the record:

* * /

3.3 function comments

3.3.1 function header comments

The function header comments should include function name, function function, entry parameter, exit parameter and so on. If necessary, you can also add the author, creation date, modification record (remarks) and other related items. The function header note is released at the top of each function and is included in the format "/ * … * /". The name of the function should be abbreviated to Name (), without adding, exit parameters and other information.

/ *

Function name:

Function function:

Entry parameters:

Export parameters:

Note:

* * /

3.3.2 Code comments

Code comments should be placed immediately above or to the right of the commented code, not below. If you put it at the top, you need to separate it from the code above with a blank line. Generally, a small number of comments should be added at the end of the line of the commented statement, multiple comments within a function should be aligned to the left; more comments should be added to the top and the comment line should be aligned to the left of the commented statement. The function code is annotated in the format "/ /... / /".

In general, branch statements (conditional branches, loop statements, and so on) must be commented. The right side of the block end line "}" should be marked "end of …" indicating the end of the block. Especially in cases of multiple nesting

3.4 comments for variables, constants, macros

Identifiers of the same type should be defined centrally, and their commonalities should be uniformly annotated one line before the definition. A comment on a single identifier is added at the end of the line of the definition statement. Global variables must be annotated in detail, including their functions, range of values, which functions or procedures to access it, and considerations when accessing it. The comments are in the format of "/ /... / /".

4. Function

4.1 principles of function design

The basic requirements of the function:

1) encapsulation

1) correctness: the program should realize the function required by the design.

2) Stability and security: the program runs stably, reliably and safely.

3) testability: the program is easy to test and evaluate.

4) Specification / readability: the writing style and naming rules of the program conform to the specification.

5) Extensibility: the code leaves room and interfaces for the next upgrade extension.

6) Global efficiency: the overall efficiency of the software system is high.

7) Local efficiency: the efficiency of a module / sub-module / function is high.

The basic principles for compiling functions:

1) try to limit the size of a single function to 200 lines (excluding comments and blank lines). A function performs only one function.

2) the number of local variables of the function is generally no more than 5-10.

3) A blank line between the local variable definition area and the function implementation area (including variable initialization) within the function.

4) the function name should accurately describe the function of the function. The verb-object phrase is usually used to name the function that performs an operation.

5) the return value of the function should be clear, especially the meaning of the error return value should be accurate.

6) do not return a variable that is different from the function return value by using the default conversion mode or the forced conversion mode of the compiler system.

7) reduce recursive calls to functions themselves or between functions.

8) try not to use the parameters of the function as working variables.

4.2 function definition

1) if a function has no entry parameters or exit parameters, it should be explicitly declared by void.

2) there should be one space and only one space between the function name and the exit parameter type definition.

3) there is no space between the function name and parentheses ().

4) the formal parameter of the function must be clearly defined.

5) the function of multiple parameters, and the latter shape adds a space between the comma separators of the previous parameter.

After reading the above, do you have any further understanding of the C language programming specification? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report