Home>Article>Backend Development> Do you know what the identifier naming rules of C language are (detailed explanation)

Do you know what the identifier naming rules of C language are (detailed explanation)

烟雨青岚
烟雨青岚 forward
2020-07-07 13:31:03 24636browse

The first step in learning C language is the identifier. Do you know the naming rules for identifiers? Let’s take a look below.

Do you know what the identifier naming rules of C language are (detailed explanation)

Identifiers in C language should generally follow the following naming rules:

1Identifiers must It starts with the letters a~z, A~Z or underscore, and can be followed by any number (can be 0) characters. These characters can be letters, underscores and numbers. Other characters are not allowed to appear in the identifier.

2Identifiers are case-sensitive

3The length of the identifier,c89 specifies within 31 characters, c99 specifies within 63 characters

4The keywordin C language has special meaning,cannot be used as an identifier

5Customized identifiers are best Take the stringwith a certain meaning to facilitate memory and understanding.

Actually, this is similar to most language regulations. Only by developing good naming conventions can you write more readable programs.

The above variable naming rules are stipulated by C language grammar and must be followed. Failure to follow them is wrong.

In addition, in actual development, programmers have also formed a "habit" of variable naming, or a conventional variable naming specification. Although they are not restricted by grammar, they abide by these "habits". "De facto standards" will make the code more professional and more sophisticated.

1) It is forbidden to use single letters as variable names (such as a, b, c, d, i, j, k, m, n...). The reason why single letters are used in the previous explanation is just to facilitate the explanation, so as not to overwhelm the focus. You can use it when you first start learning, but don't stick to single letters forever.

Then why not define it as a single letter? The reason is simple - it makes no sense! You define a variable a, how do others know what a means? Even if there are notes, it is inevitable that they will be forgotten.

So when defining variables, it is best to have a clear meaning in the variable name, which can improve the readability of the code. Whether you write the program yourself or have others read your program, you will feel comfortable.

In actual programming, English words or abbreviations of English words are often used as variable names

There are also certain rules for abbreviations:Usually shorter words can be removed by removing " Vowels" form abbreviations

For example, count which represents "number" can be abbreviated to cnt;

For longer words, several letters in the word can be used to form abbreviations;

There are also generally recognized abbreviations for some words

For example: temp can be abbreviated to tmp; //temp means "temporary"

flag can be abbreviated to flg; //flag is "flag" Bit" means

statistic can be abbreviated to stat; //statistic means "statistics"

increment can be abbreviated to inc;

message can be abbreviated to msg;

If the variable name consists of multiple words and does not require abbreviations, then the first letter of each word must be capitalized and separated by underscores if necessary.

2) But this does not mean that single letters cannot be used. Some variables themselves have no meaning, and we cannot assign meaning to them. In this case, single letters can be used. For example, when learning loop statements later, there is a loop variable in it, which is defined as i, j, k. This is already recognized.

But in most cases, variables have meanings when programming. At this time, an English word or an abbreviation of an English word should be used as its name.

Thank you everyone for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/huqiaolong/article/details/80469306

Recommended tutorial: "C Language"

The above is the detailed content of Do you know what the identifier naming rules of C language are (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete