Common programming Specification for C language

Source: Internet
Author: User
Tags naming convention

1 typesetting

1-1 You must add a blank line between the relative independent blocks and the variable description.
Example: The following example does not conform to the specification.
if (!valid_ni (NI))
{
...//program code
}
Repssn_ind = Ssn_data[index].repssn_index;
Repssn_ni = Ssn_data[index].ni;
Should be written as follows
if (!valid_ni (NI))
{
...//program code
}
Repssn_ind = Ssn_data[index].repssn_index;
Repssn_ni = Ssn_data[index].ni;
1-2: Longer statements (>80 characters) are divided into multiple lines of writing, long expressions to divide new rows at low-priority operators,
The operator is placed at the beginning of the new line, and the new line is indented to make the layout tidy and the statement readable.
Example:
Perm_count_msg.head.len = No7_to_stat_perm_count_len

  • Stat_size_per_fram * sizeof (_UL);
    act_task_table[frame_id * Stat_task_check_number + index].occupied
    = stat_poi[index].occupied;
    Act_task_table[taskno].duration_true_or_false
    = Sys_get_sccp_statistic_state (Stat_item);
    Report_or_not_flag = ((TaskNo < Max_act_task_number)
    && (N7stat_stat_item_valid (Stat_item))
    && (Act_task_table[taskno].result_data! = 0));
    1-3: Multiple phrase sentences are not allowed to be written in one line, that is, a single statement is written on a line.
    Example: The following example does not conform to the specification.
    rect.length = 0; rect.width = 0;
    Should be written as follows
    rect.length = 0;
    rect.width = 0;
    1-4: If, for, does, while, case, switch, default, and so on, the statement takes a line, and if, for, does, while
    Parentheses {} are added to the execution statement portion of the statement, no matter how much.
    Example: The following example does not conform to the specification.
    if (PUSERCR = = NULL) return;
    Should be written as follows:
    if (PUSERCR = = NULL)
    {
    Return
    }
    1-5: Alignment uses only the SPACEBAR and does not use the TAB key.
    Note: In order to avoid reading the program with different editors, the layout of the program is caused by the different number of spaces set by the TAB key.
    Not neat, do not use BC as the editor version, because BC will automatically turn 8 spaces into a TAB key,
    As a result, versions that are combined with BC will be indented in most cases.
    1-6: The delimiter of the block (such as the curly braces ' {' and '} ') of the C + + language should be exclusive for each row and be in the same column,
    Also align to the left of the statement that references them. At the beginning of the function body, the definition of the class, the definition of the structure, the definition of the enumeration to
    And if, for, do, while, switch, case statements in the program are to use the indentation as above.
    Example: The following example does not conform to the specification.
    For (...) {
    ...//program code
    }
    if (...)
    {
    ...//program code
    }
    void Example_fun (void)
    {
    ...//program code
    }
    Should be written as follows.
    For (...)
    {
    ...//program code
    }
    if (...)
    {
    ...//program code
    }
    void Example_fun (void)
    {
    ...//program code
    }
    1-7: A line program to less than 80 characters is appropriate, do not write too long.

2 Notes

2-1: In general, the source program effective annotation amount must be above 20%.
Note: The principle of annotation is helpful to the reading comprehension of the program, in the addition of places are added, comments should not be too much or not
Too few, the annotation language must be accurate, understandable, concise.
2-2: Write code side comments, modify the code at the same time to modify the corresponding comments to ensure that the comments and code consistency. No longer
Useful comments to delete.
2-3: Comments should be similar to the code they describe, and comments on the code should be placed above or to the right (comments on a single statement)
Adjacent position, not to be placed below, if placed above the code above should be separated by a blank line.
Example: The following example does not conform to the specification.
Example 1:
/* Get replicate Sub system index and NET indicator */

Repssn_ind = Ssn_data[index].repssn_index;
Repssn_ni = Ssn_data[index].ni;
Example 2:
Repssn_ind = Ssn_data[index].repssn_index;
Repssn_ni = Ssn_data[index].ni;
/* Get replicate Sub system index and net indicator /
Should be written as follows
/
Get replicate sub system index and net indicator /
Repssn_ind = Ssn_data[index].repssn_index;
Repssn_ni = Ssn_data[index].ni;
2-4: For all variables, constants with physical meaning, if their names are not fully self-explanatory, they must be
Be annotated to explain its physical meaning. The comments for variables, constants, and macros should be placed either adjacent to or to the right.
Example:
/
Active Statistic task number */

Define Max_act_task_number 1000define max_act_task_number/* Active statistic TASK number /
2-5: Note the same indentation as the described content.
Description: Can make the program layout neatly, and convenient annotation reading and understanding.
Example: The following example, typesetting is not neat, reading a little bit inconvenient.
void Example_fun (void)
{
/
Code One comments /
Codeblock One
/
Code comments /
Codeblock
}
Should be changed to the following layout.
void Example_fun (void)
{
/
Code One comments /
Codeblock One
/
Code comments /
Codeblock
}
2-6: Comment Marks to the right of the end line of the block to indicate the end of a program block.
Description: This makes the code clearer and easier to read when the code snippet is longer, especially multiple nesting.
Example: See the example below.
if (...)
{
Program code
while (Index < MAX_INDEX)
{
Program code
} /
End of while (Index < MAX_INDEX) ///Indicates the end of this bar while statement
} /
End of if (...) ///indicate which of the IF statements end
2-7: note format as uniform as possible, it is recommended to use "/
...... */”。
2-8: note should consider the program easy to read and the appearance of the factors, the use of the language if both Chinese and English, it is recommended to use more
Chinese, unless it can be expressed in very fluent and accurate English.
Note: Note language is not uniform, affect the readability of the program and the appearance of typesetting, for the consideration of maintenance personnel, it is recommended to use
Text

3 identifier naming

3-1: The name of the identifier to be clear, clear, have a definite meaning, while using the complete word or everyone is basically understandable
Abbreviations to avoid misunderstanding.
Note: Shorter words can be shortened by removing the "vowel", and the longer word may be indented by the first few letters of the word.
Some words have a well-known abbreviation.
Example: The abbreviations of the following words can be basically recognized by everyone.
Temp can be abbreviated as TMP;
Flag may be abbreviated as FLG;
Statistic can be abbreviated as stat;
Increment can be abbreviated as INC;
Message can be abbreviated as MSG;
3-2: If a special convention or abbreviation is used in a name, a comment will be provided.
Note: It should be at the beginning of the source file, the abbreviations or conventions used in the file, especially the special abbreviations,
The necessary comment description.
3-3: Their own unique naming style, to be consistent from start to finish, can not change back and forth.
Description: A personal naming style that can be used only if it complies with the naming rules of the project group or product group. (i.e.
There is no place in the naming convention where you can have a personal name style).
3-4: For variable name, prohibit to take single character (such as I, J, K ... ), it is suggested that in addition to having specific meanings, it is possible to
Variable type, data type, but I, J, K are allowed for local cyclic variables.
Description: Variables, especially local variables, are easy to knock if represented by a single character (e.g. I writes J), while compile time
And can not be checked out, it is possible for this small mistake to spend a lot of time to find the wrong.
Example: the definition of the local variable name shown below can be used for reference.
int Liv_width
The variable name is interpreted as follows:
L local variable (other: G global variable) ... )
I data type (Interger)
V Variable (Variable) (Other: C constant (Const) ... )
Width variable Meaning
This prevents local variables from having duplicate names with global variables.
3-5: The naming specification must be consistent with the system style used and be unified in the same project, such as with UNIX
Full lowercase underline style or case-by-case blending, do not use casing and underline in a way that
M_ and g_ that are special identifiers, such as identifying member variables or global variables, are then allowed by the way they are mixed with uppercase and lowercase.
Example: Add_user not Allowed, Add_user, AddUser, M_adduser allowed.
3-6: In the same software product, should plan the interface part identifier (variable, structure, function and constant) naming,
Prevents conflicts from being compiled or linked.
Description: Identifiers for interface parts should be more restrictive to prevent collisions. If you can specify the variables and constants of the interface section
Before adding "module" logo.
3-7: Use the correct antonyms group to name a variable with mutually exclusive meaning or a function of the opposite action.
Description: Below are some of the antonyms groups commonly used in software.
Add/remove Begin/end Create/destroy
Insert/delete First/last Get/release
Increment/decrement Put/get
Add/delete Lock/unlock Open/close
Min/max old/new Start/stop
Next/previous Source/target Show/hide
Send/receive source/destination
Cut/paste Up/down
Example:
int min_sum;
int max_sum;
int Add_user (BYTEuser_name);
int Delete_user (BYTE
USER_NAME);

4 readability

4-1: Note the precedence of the operator, and use parentheses to clarify the order of operations of the expression and avoid using the default precedence.
4-2: The size of the function should be limited to 200 lines as far as possible.

Common programming Specification for C language

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.