C language writing specification

xiaoxiao2021-03-06  25

C language writing specification CAIYI9000 original

I have developed under the C language, and I have accumulated some experience of code writing for your reference:

1.

1.1 Symbol Naming Rules 1.1.1 Symbol names include module names, constant names, label names, subroutines, and more. These names should be able to reflect the actual things it represent, have certain significance, so that they can see the name of the name to help understand the program function. Named Hungarian nomenclature. The rules are as follows: (1) All macro definitions, enumerations, and const variables, named by uppercase letters. Separate each word with underlined words. (2) The first letter of each word in the compound word. Avoid using underscores except for rules 5.1.1.1. (3) Class, type definition, and enumeration names of the first letter capital. (4) The function name is a compound word. The first word uses all lowercases, and then each word uses the first letter uppercase, other letters lowercase; if it is a single word, all lowercase methods are used. (5) The cyclic variables can be used, and I, J, K, etc. are not limited by the above rules. (6) The member variable of the class should be the beginning of M_. (7) The global variable word head is g_. (8) Temporary variable word head is TMP_. (9) Naming of variables in the structure, the specific meaning naming principle of variables (10) uses lowercase letters to represent the type of variable, the next letter of the prefix with uppercase. Table 1 Word Type Word Type CH Char L Longi Integer U UNSIGNED B Boolean P P P P P P P P P P P P P P P P P P P POINTERD DOUBLE STRINGST STRUCTURE SZ ASCII STRINGET STRUCTURE SZ ASCII STRINGET STRUCTURE SHORT INTH HANDLE X, Y Sizes DW DWORD FN Function

Table 2 Word Head Variable Name Word Head Variable Name Task Task Sig SignalsB Binary Semaphores WD Watchdogsm Mutual ExclusionTM Timer Sc Counting Semaphores MSG MessagePipe Pipe

Example: #define array_size 24 / * rule 5.1.1.1 * /

INT g_iflag; class myclass / * rule 5.1.1.3 * / {};

Void Somefunc () / * Rules 5.1.1.2 and 5.1.1.4 * / {

INT NARRAY [Array_Size]; unsigned char uchbyte; char szname []; char * pszname = SZNAME;}

(11) Some words (such as P and u) can be combined with other territories.

Example: WDOG_ID WDID; WDOG_ID G_WDID; / * Global WatchDog ID, so the G_ start * /

1.1.2 The length of the name is generally not too long or too short. Excessive names increase the workload, make the program logic process vague; too short names unable to express the actual meaning of symbols. Conventional range: 3-31;

1.2 Data and Function Description 1.2.1 Data Description The order should be normalized, making data properties easy to find, and also contribute to testing, tap and maintenance. The predendation of the description should be fixed, should be sorted by logical function, and the following order is recommended within the logical function block: integer, realistic description, character description, logical description. 1.2.2 If a complex data structure is designed, the meaning of its variable should be described by comment. 1.2.3 Use an abnormal statement in a statement of a function. Such as: void f () Throw (TooBig, Toosmall, Divzero); When a function is declared, the exception it throws out, and the user who is easy to function can understand which exceptions may occur. 1.3 Program Notes 1.3.1 Program Note is one of the important means of communication between programmers and future program readers, and comments are divided into file annotations, function comments, and feature comments. 1.3.2 Note Note: - Note The number of annotation lines accounts for 1/3 to 1 / 2.1.3.3 file comment at the entire source program, the end of the entire source program, the comment is started Program body. It includes: - the program title. - Objective, function description. - Document author, final modification date, etc. example:./********************************************** ********************* (empty one) Title: DEMO.C function: Test VxWorks's various system calls. Description: This program tests various VxWorks System call function. Including the creation of tasks (TAKS), the hanging and task is synchronized through the signal light through the message queue. The program creates two tasks: a high priority task and a low priority task. The two tasks are synchronized through a binary signal light, communicating via the message queue. Current version: XX modification information: 2000.06.05 john, initial version 2000.07.05 tom, bug xxxx fixed ************************************ ******************************************* (empty 2 lines, start program body)

1.3.4 Function Note Normally placed at the beginning of each function or process, which should give a function or process as an overall instructions for the understanding that the program itself has booting. Generally include the following entry: - Module header. - Instructions for this module function and purpose. - Call format - Interface Description: Includes input, output, return value, exception. - Algorithm. Some complex algorithms are used in the module. Example: file: // (/ Note At the beginning of the previous function, two lines) (the beginning of the note and the last function of the last function, two lines) / **************** *********************************************************** * title: assignmentComplete function: BSC => MSC message generating function generates assignment_complete assignment complete message (BSMAP message) format:. int assignmentComplete (int iCellId, int iServiceChannnelNum, char * pszMSGData) throw (exception1, exception2) input: int iCellId : The cell recognition of MS ICellid value: 0x00 --- 0xFF

INT ISERVICECHANNNELNUM: MS accounting for business channel number output: char * pszmsgdata: Fire with completion message data return value: 0x00 Normal exception: Exception1 exception 1, Exception2 exception 2 ************ *********************************************************** ***** / (After the comment, the program is started directly, no time.) 1.3.5 Functional Note In the source program, it is used to describe what work after the subsequent statement or program segment is explained below. What to do, or how to perform the following statement. Not to explain how to do it below, because explains how often is repeated with the program itself. Example: / * Add Amount to Total * / Total = Amount Total; this comment is just repeating the following programs, which does not work for understanding its work. The following comments help readers understand. / * Put the monthly sales Amount to the annual sales Total * / total = Amount Total; 1.4 Function Writing should be as short as possible, generally no more than two screens to facilitate debugging and understanding. 1.5 Structure Structure To ensure the clarity and program readability of the statement structure, pay attention to the following aspects when writing software programs: - Only write a statement in one line, and use spaces, blank lines and shifts to ensure clear Visual effect. - Each nested function block, uses a Tab indent (can be set to 4 spaces), the braces must be placed next to the conditional statements, and they are alone, which is easy to match: if there is a program as follows : For (i = 1; i

Q / ECC / BJ 010-2001Work = a [t]; a [t] = a [i]; a [i] = work;}}}

转载请注明原文地址:https://www.9cbs.com/read-65449.html

New Post(0)