I've seen this in countless C/C++ header and source files.
Apart from personal preference, what's the meaning of each of these different notations?_NAME
__NAME
_NAME_
__NAME__
NAME_t
Quite honestly, the only one I comprehend is _NAME
, which is a private member of a class (at least in C# it is).
Can someone explain the difference among them?
And if there are any other noticeable notations as such, please mention them (even in other similar languages).Thanks!
Names that begin with _
or __
are typically for reserved identifiers, and you should not define/create identifiers that begin with that convention. (_ with Upper Case following)
From C99 standard, 7.1.3 - "Reserved Identifiers"
— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
From C++03 Standard (amendment to C++98), section 17.4.3.1.2 - "Global Names"
— Each name that contains a double underscore (_ _) or begins with an underscore followed by an upper- case letter (2.11) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace
Except for NAME_t
, all of these names are reserved for "the implementation", that is, the standard C++ library code. You are forbidden to use such names in your own code.
Section 17.6.3.3.2 "Global names" §1 states:
Certain sets of names and function signatures are always reserved to the implementation:
Each name that contains a double underscore or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.
Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
(Note that these rules forbid header guards like __MY_FILE_H
which I have seen quite often.)
Types ending in _t
are reserved by POSIX.
精彩评论