Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to name identifiers in C language

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge of how to name identifiers in C language. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

1. the naming of identifiers should be clear, clear and have a clear meaning, and use complete words or abbreviations that everyone can understand, so as to avoid misunderstanding.

Description: give a descriptive name as much as possible, don't save space, and it's more important to let others understand your code quickly.

Example: good naming:

Interror_number

Intnumber_of_completed_connection

Bad naming: use vague abbreviations or random characters:

Intn

Intnerr

Intn_comp_conns

2. Except for common abbreviations, do not use word abbreviations, do not use Hanyu pinyin

Description: shorter words can be abbreviated by removing "vowels", longer words take the first few letters of words to form abbreviations, some words have accepted abbreviations, and the abbreviations of commonly used words must be unified. The abbreviations of the words in the agreement are consistent with the agreement. The special abbreviations used by a system should be explained uniformly at the gaze or somewhere.

Examples: some common examples that can be abbreviated:

Argument can be abbreviated to arg

Buffer can be abbreviated to buff

Clock can be abbreviated to clk

Command can be abbreviated to cmd

Compare can be abbreviated to cmp

Configuration can be abbreviated to cfg

Device can be abbreviated to dev

Error can be abbreviated to err

Hexadecimal can be abbreviated to hex

Increment can be abbreviated to inc,

Initialize can be abbreviated to init

Maximum can be abbreviated to max

Message can be abbreviated to msg

Minimum can be abbreviated to min

Parameter can be abbreviated to para

Previous can be abbreviated to prev

Register can be abbreviated to reg

Semaphore can be abbreviated to sem

Statistic can be abbreviated to stat

Synchronize can be abbreviated to sync

Temp can be abbreviated to tmp

3. A unified naming style should be maintained within the product / project group.

Note: both Unix like and windows like styles have their own fans, and the product should choose one of them according to its own deployment platform and be consistent within the product.

Exception: even if Hungarian nomenclature is used before the product, the new code should not be used.

4. Name variables with mutually exclusive meaning or functions of opposite actions with correct antonyms.

Example:

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 copy/paste up/down

5. Try to avoid numbering in the name, unless it is logically necessary

Example: it is named below, which makes people confused.

# define EXAMPLE_0_TEST_

# define EXAMPLE_1_TEST_

It should be changed to meaningful word naming.

# define EXAMPLE_UNIT_TEST_

# define EXAMPLE_ASSERT_TEST_

6. The names of modules, projects, products and departments should not be prefixed before the identifier.

Description: many existing codes have become accustomed to adding the module name to the file name, which is similar to the Hungarian nomenclature, which makes the file name unreadable and brings the following problems:

1 the first thing you see is the module name, not the real file function, which hinders reading

2 the file name is too long

(3) the file name is bound to the module, which is not conducive to maintenance and migration. If foo.c moves from module a to module b after refactoring, and if there is a module name in foo.c, you need to change the file name from a_module_foo.c to b_module_foo.c

7. Naming convention

7.1 File naming is based on lowercase characters

Note: because different systems handle file name case differently (such as MS's DOS and Windows systems are not case-sensitive, but Linux systems are case-sensitive), code file naming is recommended to be named in all lowercase letters.

7.2 Global variables should be prefixed with "g _"

7.3 static variables should be prefixed with "s _"

Note: add g _ prefix or s _ prefix for the following reasons:

First of all, global variables are very dangerous, and prefixes make global variables more eye-catching, prompting developers to be more careful about their use.

Second, fundamentally speaking, you should try not to use global variables, and adding the g _ and s _ prefixes will make the names of global variables look ugly, thus prompting developers to use global variables as little as possible.

7.4 single-byte named variables are prohibited, but I, j, k are allowed to be defined as local loop variables

7.5 Hungarian nomenclature is not recommended.

7.6 the function should be named after the action to be performed by the function, usually with the structure of verb or verb + noun.

7.7 for the definition of constants such as numeric values or strings, it is recommended to use all uppercase letters and underscore'_ 'between words (enumerations are also recommended to be defined in this way).

Example:

# define PI_ROUNDED 3.14

7.8 Macro definitions cannot start and end with an underscore'_ 'except for special identity definitions such as header files or compilation switches.

Note: generally speaking, the macros at the beginning and end of'_ 'are internal definitions. ISO/IEC 9899 (commonly known as C99) has the following description (6.10.8 Predefined macro names):

These are all the contents of the article "how to name identifiers in C language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report