Zerglurker's C-language tutorial-naming conventions

Source: Internet
Author: User

Today, I'll talk about naming rules in C + +.

Naming conventions or naming restrictions can be applied to function naming, variable naming, constant naming, struct naming, custom type naming, and class naming in C + +, object naming, template class or object naming, and so on.

First: The first character of a name can only be a letter or an underscore.

int _,_a,a1,_9;//Legal int 1_a,1_, $S;//illegal

Note: Only one underlined name, in C/s + +, is a valid name!

Second: The name can contain numbers

Article Three: names cannot contain symbols other than underscores, letters, and numbers

Fourth: The name is case sensitive

That is, name and name are two different names (in the database language, the two are often equivalent)

Fifth: The name cannot be the same as the keyword

For example, you cannot name a variable called return. (The concept of the keyword is described in detail in the next section)

Implied limitations:

1 names should not be the same as system functions, system variables, library function names, the global variables provided by the library.

For example, it is best not to name a printf function, otherwise it is easy to have a conflict in the link stage (which is covered later in the code compilation process)

2 main can only be used for the entry function (or the starting function) named, not for other functions, variables

When you violate this restriction, you can cause the compiler to have problems during the link phase.

In addition, the compiler basically does not limit the length of the name (at least 256-byte-length names), but it is best not to have more than 32 named lengths.

Because in some of the development of microcontroller, the old-fashioned compiler for more than 31 length of the name, will ignore the later part.

Also, in the libraries mentioned later, if the exported name is too long, it may cause link problems

Named suggestions:

1 proper use of underscores to separate the names of individual words

2 It is not recommended to begin naming functions and variables with an underscore, as this may conflict with some system reserved names or variable names

3 use uppercase and lowercase to separate parts of a name

4 macro names should always be named in uppercase letters (the meaning of the macro will be explained later)

Currently more commonly used naming conventions:

Hungarian nomenclature helps people understand variables by adding prefixes

Camel (Camel) nomenclature If a name consists of multiple words, the first word is all lowercase, the first letter of each word is capitalized, and the other letter is lowercase

If you have capitalized the first character of the initial word, hmm, it doesn't matter, it's Pascal's name law.

Underline naming uses underscores to separate parts of a name

All right, we're here today.

You can try out the various names in your code yourself.


Zerglurker's C-language tutorial-naming conventions

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.