单下划线和双下划线前缀

在程序中定义下划线,双下划线,及“str”,“is","to"均已为ansiC标准委员会所保留,如果自行定义就犯了错误。
另:你最好不要使用所有标准库中定义的标识符(变量名,宏,函数名……),如果你特别喜欢用的话,建议你在变量名的前面加上你的个性前缀(如NMD_,FUCK_等……,什么?我骂人?NO,清华大学的学生—伏明瞎都不知道,我哪会知道是什么意思)或另加后缀_DOG,_CS……。

以下是Richard Heathfield Lawrence Kirby等建议(客气)不要使用的标准标识符或前缀:
E[0-9]*    E[A-Z]*     is[a-z]*
LC[A-Z]*   mem[a-z]*   NDEBUG
Offsetof   raise       SIG[A-Z]*
str[a-z]*  to[a-z]*    wcs[a-z]*
_*

特别提一句,许多人喜欢定义下划形式标识符,很危险。
_MY_HEARD_H
_MY_ALLOC_H
_YOUR_MOTHER_PP_H

 

Following contents are from Sam's book "C++ Premier Plus (5th edition)", page 67.

Names beginning with two underscore characters or with an underscore character followed by an uppercase letter are reserved for use by the implementation—that is, the
compiler and the resources it uses. Names beginning with a single underscore character are reserved for use as global identifiers by the implementation.

The next-to-last point is a bit different from the preceding points because using a name such as __time_stop or _Donut doesn’t produce a compiler error; instead, it leads to undefined behavior. In other words, there’s no telling what the result will be. The reason there is no compiler error is that the names are not illegal but rather are reserved for the implementation to use.

The final point differentiates C++ from ANSI C (C99), which guarantees only that the first 63 characters in a name are significant. (In ANSI C, two names that have the same first 63 characters are considered identical, even if the 64th characters differ.)


Here are some valid and invalid C++ names:

int poodle; // valid
int Poodle; // valid and distinct from poodle
int POODLE; // valid and even more distinct
Int terrier; // invalid -- has to be int, not Int
int my_stars3 // valid
int _Mystars3; // valid but reserved -- starts with underscore
int 4ever; // invalid because starts with a digit
int double; // invalid -- double is a C++ keyword
int begin; // valid -- begin is a Pascal keyword
int __fools; // valid but reserved – starts with two underscores
int the_very_best_variable_i_can_be_version_112; // valid
int honky-tonk; // invalid -- no hyphens allowed


 


 

你可能感兴趣的:(C)