C代码中的命名方式总结和改进

本文介绍了编码规范中宏、结构体、函数及变量的命名规则,并详细解释了匈牙利命名法的类型前缀、类型修饰符及作用域修饰符等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 宏全部使用大写字母--------宏大写
  2. 结构体名字全部使用typedef,typedef之后的名字为大写-------结构体别名大写
  3. 函数名全部使用小写字母,单词之间使用下划线分割-------函数名小写,单词之间下划线(1)
  4. 函数名第一个单词全小写,后面单词的首字母大写,单词之间使用下划线---------函数名首单词小写,后面单词首字母大写(2)
  5. 变量名也是全部使用小写,单词之间使用下划线分割-------变量名小写,单词之间下划线(1)
  6. 变量名第一个单词全小写,后面单词的首字母大写,单词之间使用下划线---------变量名单词小写,后面单词首字母大写(2)

变量的命名前后缀规则如下(这是匈牙利命名法的前后缀格式,以后其他地方使用上述的总结,前后缀使用匈牙利的):

The type prefix indicates the data type of the variable.

Type prefixMeaningExample
bbooleanbool bHasEffect;
c (or none*)classCreature cMonster;
chchar (used as a char)char chLetterGrade;
ddouble, long doubledouble dPi;
eenumColor eColor;
ffloatfloat fPercent;
nshort, int, long
char used as an integer
int nValue;
sstructRectangle sRect;
strC++ stringstd::string strName;
szNull-terminated stringchar szName[20];

The following type modifiers are placed before the prefix if they apply:

Type modifierMeaningExample
aarray on stackint anValue[10];
ppointerint* pnValue;
padynamic arrayint* panValue = new int[10];
rreferenceint rnValue;
uunsignedunsigned int unValue;

The following scope modifiers are placed before the type modifier if they apply:

Scope modifierMeaningExample
g_global variableint g_nGlobalValue;
m_member of classint m_nMemberValue;
s_static member of classint s_nValue;

补充:

  • p ---------for a pointer. A pfoo would be a pointer to data item of type FOO
  • pp------- for a pointer to a pointer.
  • h --------for a heap handle. This is a pointer to a pointer that points at a data item recorded within a heap.
  • rg -------for an unstructured array containing data items of a certain data type. An rgfoo would be an array that contains data of type foo. Individual elements would be selected by an ifoo index variable.
  • mp -------for an array which is used to map from one data type to another.Eg. A mpdochdod would be indexed via a doc index. The expression mpdochdod[doc] would produce a handle to a document descriptor.
  • dn-------- for an array whose elements that describes a meaningful index such as an opcode. If the meaningful index were an OP type, a data structure of type EOP (entries for OP) would be defined, and a dnop array would be defined which describes what each individual op means

Max - added to an index data instance which records the actual size of an array or data block.
eg. the declaration of a an array of type FOO in C would be:
FOO rgfoo[ifooMax];

Mac - added to an index data instance to indicate the limit of an actual usage within an array.Mac stands for current maximum. It is invariant that ifooMac <= ifooMax. ifooMac == ifooMax is the condition which is true when all entries within an array are in use.

First - added to an index or pointer which designates the first element within a range that may be validly processed

Last - added to an index or pointer which designates that last entry within a range that may be validly processed.

Lim - stands for limit. An ifooLim is equal to ifooLast + 1 and designates the location where a new foo item could be recorded in an array or data block.



转载于:https://www.cnblogs.com/jack204/archive/2011/09/07/2170408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值