C Language Programming specification-naming conventions

Source: Internet
Author: User
Tags function prototype naming convention uppercase letter

Reprinted from: http://www.cnblogs.com/wfwenchao/p/5209197.html

C is a plain language, and the name you use should be the same. Unlike Modula-2 and Pascal programmers, C programmers do not use "smart" names such as "Thisvariableisatemporarycounter". C Programmers should call it "TMP", which is easier to write and less difficult to understand.
However, it is tricky to deal with complex situations, and it is necessary to give a descriptive name to the global variable. It is a short-sighted behavior to call a global function "foo". The global function is the same, if you have a function that counts the current number of users, you should name it "Count_active_user ()" or a similar name in simple order, and should not be named "Cntusr ()".
3.1 Three popular naming laws

At present, there are four kinds of naming laws in the industry: hump nomenclature, Hungarian nomenclature, Pascal's nomenclature and Underline nomenclature, among which the first three are the more popular nomenclature.

(1) Hump order method. As its name implies, it means mixing uppercase and lowercase letters to form the names of variables and functions. For example, the following is the same function that is named with the camel-named method and the underscore method, respectively:
Printemployeepaychecks ();
Print_employee_paychecks ();

The first function name uses the hump naming method, and each logical breakpoint in the function name is marked with an uppercase letter. The second function name uses the underscore method, and each logical breakpoint in the function name is marked with an underscore.

The hump nomenclature has become more and more popular in recent years, and it is used in many new libraries and in environments like Microsoft Windows. On the other hand, the underscore is popular after the advent of C, in many of the old Programs and UNIX environment, it is very common use.

(2) Hungarian nomenclature. Widely used in environments such as Microsoft Windows. The naming convention for variables (and macros) used in Windows programming is the Hungarian nomenclature, which is proposed by Charles Simonyi, a competent Microsoft programmer. Simone.

The Hungarian nomenclature identifies the scope, type, and so on, by prefixing the variable name with the corresponding lowercase letter symbol identifier. These symbols can be used in multiple ways, in the order of first m_ (member variables), re-pointers, re-simple data types, and so on. The advantage of this is that it can increase the readability of the program and facilitate the understanding and maintenance of the program.

For example: M_lpszstr, which represents a long pointer member variable that points to a string that ends with a 0 character.
The key to the Hungarian nomenclature is that the name of the identifier is prefixed with one or more lowercase letters, followed by a word or combinations of words that are capitalized in the first letter, which indicates the purpose of the variable.

(3) Pascal (Pascal) nomenclature. Similar to the Hump nomenclature, the difference is that the hump naming method is the first letter lowercase, and the Pascal nomenclature is capitalized, such as:
DisplayInfo ();
String UserName;
Both use Pascal's nomenclature.

(4) Summary of three naming conventions: MyData is an example of Pascal's name; MyData is a hump-named method, the first letter of the first word in lowercase, and the first letter of the following word capitalized, looks like a camel; Imydata is a Hungarian nomenclature, Its lowercase i describes its form, followed by the same name as Pascal, indicating the purpose of the variable.
3.2 Basic principles of the nomenclature

(1) The name of the identifier should be clear, clear, have a definite meaning, while using the complete word or everyone can understand the basic abbreviation, to avoid misunderstanding-try to use English words or all Chinese spelling, if there are English words and Chinese mixed definition, the use of the hyphen "_" will be English and Chinese cut open. Shorter words can be abbreviated by removing the "vowel", and the long Word may be abbreviated by the first few letters of the word; some words are generally recognized abbreviations. For example: Temp->tmp, FLAG->FLG, Statistic->stat, Increment->inc, message->msg and other abbreviations can be basically recognized by everyone.

(2) If a special convention or abbreviation is used in a name, a note shall be provided. The abbreviations or conventions used in the file, especially the special abbreviations, should be annotated as necessary at the beginning of the source file.

(3) their own unique naming style, to be consistent from start to finish, can not change back and forth. The personal naming style can only be used if it conforms to the naming rules of the project group or product group. (That is, there is no specified place in the naming convention to have a personal naming style).

(4) For the variable name, prohibit to take a single character (such as I, J, K ...), it is suggested that in addition to have a specific meaning, but also to indicate its variable type, data type, etc., but I, J, K for the local loop variable is allowed. variables, especially local variables, are easy to strike if they are represented by a single character (e.g. I write j), and are not checked at compile time, and it is possible to spend a lot of time checking for this small error.

(5) Do not define identifiers with numbers or more strange characters, unless necessary.

(6) The naming specification must be consistent with the system style used and unified within the same project.

(7) In the same software product, the interface part identifier (variables, structures, functions and constants) should be well-named, to prevent the compilation, link conflict. 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" identification.

(8) using the correct antonym group to name a variable with mutually exclusive meaning or a function of the opposite action.
Here are some of the antonyms groups that are commonly used in software.
Add/remove Begin/end Create/destroy
Insert/delete First/last G et/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 (BYTE user_name);
int Delete_user (BYTE
user_name);

(9) In addition to the compiler switch/header files and other special applications, should avoid the use of _example_test_ such as the beginning and end of the definition of an underscore.
3.3 Naming conventions for variable names

(1) The naming rules of variables require the use of the "Hungarian Law".
That is, the beginning of the letter with the type of variable, the rest of the English meaning of the variable, English abbreviation, the Chinese full spelling or the abbreviation of the whole Chinese spelling, require the first letter of the word should be capitalized.
That is: variable name = variable type + variable's english meaning (or English abbreviation, Chinese full spell, Chinese full spell abbreviation)
For non-generic variables, add a comment description to the definition, and the variable definition is as likely to be placed at the beginning of the function.
See table below:
BOOL starts with B BFLG
int begins with I iCount
short int begins with n nstepcount
Long int starts with L Lsum
Char starts with C ccount
Unsigned char starts with by
Float starts with F favg
Double starts with D Ddeta
unsigned int (WORD) starts with W Wcount
unsigned long int (DWORD) starts with a DW dwbroad
String starts with S sFileName
Start with SZ with a string ending with 0 szfilename

(2) The basic principle of naming a pointer variable is:
The basic principle of a pointer variable is: "P" + Variable type prefix + name, such as a float* type should be represented as pfstat. The basic rule for a binary pointer variable is: "PP" + variable type prefix + name. The basic rule for a triple pointer variable is: "PPP" + Variable type prefix + name.

(3) Global variables start with g_, such as a global long variable defined as G_lfailcount. That is, the variable name =g_+ the variable type + the English meaning (or abbreviation) of the variable. This rule also avoids problems caused by local variables and global variables with the same name.

(4) Static variables start with s_, such as a static pointer variable defined as S_PLPERV_INST. That is: variable name =s_+ variable type + variable's english meaning (or abbreviation)

(5) A variable in an enumeration type (enum) requires a prefix with an enumeration variable or its abbreviation. and is required in uppercase. Such as:
Enum Cmemdays
{
Emdays_monday;
Emdays_tuesday;
......
};

(6) The name of the struct, union variable is defined by the type in uppercase. and to prefix it, the naming rules for internal variables are consistent with the variable naming rules.

The structure usually begins with s, such as:
struct Scmnpoint
{
The x position of the int nx;//point
int NY; The Y position of the point
};

A consortium typically begins with u, such as:
Union Ucmlpoint
{
LONG LX;
LONG LY;
}

(7) The constant (including the wrong encoding) is named, requiring the constant name in uppercase, the constant name in English to express its meaning. When more than one word is required, the hyphen "_" connection must be used between the word and the word.
such as: #define Cm_file_not_found Cmmakehr (0x20b) where CM represents the category.

(8) A const variable requires the C_ to be added before the variable's naming rules. That is: c_+ variable naming rules; Example: const char* c_szfilename;
3.4 Naming conventions for functions

(1) The name of the function should be as far as possible in English (or English abbreviation, Chinese full spelling, Chinese full spell abbreviation) function completion functions--function name should accurately describe function functions. According to the naming principle of the dynamic-object structure, the verb in the function name is preceded, and the prefix of the function is added before the name, and the function name must not be less than 8 letters in length. The first letter of the function name is capitalized, if each word that contains two words is capitalized. If it is an OOP method, you can only have verbs (nouns are the object itself). Example:
LONG getdevicecount (...);
void Print_record (unsigned int rec_ind);
int Input_record (void);
unsigned char get_current_color (void);

(2) Avoid using meaningless or ambiguous verbs to name the function. Use process, handle, and so on to name functions, because these verbs do not specify what to do.

(3) A function prototype declaration must be used. Function prototype declarations include: reference to foreign functions and intrinsic functions, external references must be on the right side of the function Source: module name and file name, intrinsic function, as long as the comment its definition file name--and the caller in the same file (simple program) do not need comments.
You should ensure that the names, types, and types of parameters in each function declaration are the same as those in the definitions.
3.5 function parameter Naming specification

(1) Named reference variable naming specification for parameter names.
(2) In order to improve the running efficiency of the program, reduce the stack of parameters, transfer the parameters of large structures, all are passed by pointer or reference method.
(3) In order to make it easier for other programmers to identify whether a pointer parameter is an ingress parameter or an exit parameter, and for the compiler to check for errors, the const flag should be added before the entry parameter.
such as: ... cmcopystring (const char * c_szsource, CHAR * szdest)
3.6 Naming conventions for file names (including dynamic libraries, components, controls, engineering files, and so on)

The name of the filename requires the content of the file to be expressed, requiring the file name to be no less than 5 letters, and the use of filenames such as File1,myfile is strictly forbidden.

C Language Programming specification-naming conventions

Related Article

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.